gpt4 book ai didi

c# - Xamarin.Forms iOS依赖服务中的触觉反馈崩溃

转载 作者:行者123 更新时间:2023-12-01 21:54:05 31 4
gpt4 key购买 nike

我正在尝试在Xamarin.Forms应用程序中使用触觉反馈来通知用户选择更改。我找到了对iOS的引用,并为iOS做了一个依赖服务。但是,它总是崩溃,没有任何C#错误。我尝试在主线程和正在使用的线程上调用它。它甚至在try / catch块中崩溃。这是我的代码:

using System;
namespace App.Services
{
public enum HapticFeedbackType
{
ImpactHeavy, // Heavy impact
ImpactMedium, // Medium impact
ImpactLight, // Light impact
Selection, // To tick while scrolling through a scrollview or carousel
NotificationError, // When an in-app error notification occurs
NotificationWarning, // When an in-app warning notification occurs
NotificationSuccess // When an in-app success notification occurs
}
public interface IHapticFeedback
{
void PrepareHapticFeedback(HapticFeedbackType type);
void ExecuteHapticFeedback(HapticFeedbackType type);
}
}

using System;
using UIKit;

using Xamarin.Forms;

using App.Services;

[assembly: Dependency(typeof(App.iOS.Services.HapticFeedbackService))]
namespace App.iOS.Services
{
public class HapticFeedbackService : IHapticFeedback
{
HapticFeedbackHelper helper;
public HapticFeedbackService()
{
helper = new HapticFeedbackHelper();
}
public void PrepareHapticFeedback(HapticFeedbackType type)
{
helper.PrepareHapticFeedback(type);
}

public void ExecuteHapticFeedback(HapticFeedbackType type)
{
helper.ExecuteHapticFeedback(type);
}
}
//https://blog.francois.raminosona.com/add-vibrations-in-a-xamarin-ios-app/
public class HapticFeedbackHelper: IDisposable
{
private UIImpactFeedbackGenerator _impactHeavyFeedbackGenerator;
private UIImpactFeedbackGenerator _impactMediumFeedbackGenerator;
private UIImpactFeedbackGenerator _impactLightFeedbackGenerator;
private UISelectionFeedbackGenerator _selectionFeedbackGenerator;
private UINotificationFeedbackGenerator _notificationFeedbackGenerator;

public HapticFeedbackHelper()
{
_impactHeavyFeedbackGenerator = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Heavy);
_impactMediumFeedbackGenerator = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Medium);
_impactLightFeedbackGenerator = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light);
_selectionFeedbackGenerator = new UISelectionFeedbackGenerator();
_notificationFeedbackGenerator = new UINotificationFeedbackGenerator();
}


public void PrepareHapticFeedback(HapticFeedbackType type)
{
switch (type)
{
case HapticFeedbackType.ImpactHeavy:
_impactHeavyFeedbackGenerator.Prepare();
break;
case HapticFeedbackType.ImpactMedium:
_impactMediumFeedbackGenerator.Prepare();
break;
case HapticFeedbackType.ImpactLight:
_impactLightFeedbackGenerator.Prepare();
break;
case HapticFeedbackType.Selection:
_selectionFeedbackGenerator.Prepare();
break;
case HapticFeedbackType.NotificationError:
case HapticFeedbackType.NotificationWarning:
case HapticFeedbackType.NotificationSuccess:
_notificationFeedbackGenerator.Prepare();
break;
}
}

public void ExecuteHapticFeedback(HapticFeedbackType type)
{
switch (type)
{
case HapticFeedbackType.ImpactHeavy:
_impactHeavyFeedbackGenerator.ImpactOccurred();
break;
case HapticFeedbackType.ImpactMedium:
_impactMediumFeedbackGenerator.ImpactOccurred();
break;
case HapticFeedbackType.ImpactLight:
_impactLightFeedbackGenerator.ImpactOccurred();
break;
case HapticFeedbackType.Selection:
_selectionFeedbackGenerator.SelectionChanged();
break;
case HapticFeedbackType.NotificationError:
_notificationFeedbackGenerator.NotificationOccurred(UINotificationFeedbackType.Error);
break;
case HapticFeedbackType.NotificationWarning:
_notificationFeedbackGenerator.NotificationOccurred(UINotificationFeedbackType.Warning);
break;
case HapticFeedbackType.NotificationSuccess:
_notificationFeedbackGenerator.NotificationOccurred(UINotificationFeedbackType.Success);
break;
}
}

#region IDisposable
public void Dispose()
{
_impactHeavyFeedbackGenerator = null;
_impactMediumFeedbackGenerator = null;
_impactLightFeedbackGenerator = null;
_selectionFeedbackGenerator = null;
_notificationFeedbackGenerator = null;
}
#endregion


}
}

 async void LikeDown(object sender, MR.Gestures.DownUpEventArgs e)
{
LoggingController.Info("LikeDown event started...");
if (cancelReactionShowToken?.Token != null && cancelReactionShowToken.Token.CanBeCanceled)
cancelReactionShowToken.Cancel();

cancelReactionShowToken = new CancellationTokenSource();
Task task = new Task(async delegate {
await Task.Delay(800);
if (cancelReactionShowToken.Token.IsCancellationRequested)
{
cancelReactionShowToken = null;
return;
}
MainThread.BeginInvokeOnMainThread(() =>
{
reactionPopup = new SfPopupLayout();
reactionPopup.PopupView.ShowHeader = false;
reactionPopup.PopupView.ShowFooter = false;
reactionPopup.PopupView.AutoSizeMode = AutoSizeMode.Both;
reactionPopup.PopupView.ContentTemplate = new DataTemplate(() =>
{
ReactionsView view = new ReactionsView();
this.Emojis = view.Emojis;
this.ReactionTypes = view.ReactionTypes;
return view;
});
reactionPopup.ShowRelativeToView(actionsRow, RelativePosition.AlignBottom, 0, 0);


try
{
hapticFeedback.ExecuteHapticFeedback(Services.HapticFeedbackType.Selection);

} catch (Exception ex) { Console.WriteLine(ex.Message); }

//needs to be canceled so other guestures know
cancelReactionShowToken.Cancel();
}, cancelReactionShowToken.Token);
task.Start();
await task;
}

如果有人在Xamarin.Forms中使用触觉反馈有任何经验,那将有很大帮助。

最佳答案

您需要放置一个异常捕获点,并准确查看崩溃发生的位置。您已经放置了所有代码,很难弄清楚混乱的地方。它可能只是无关的东西。您还应该共享将显示异常情况的应用程序输出。

这是ElysiumLab.com的Haptic实现的示例,他们也有一个Nuget程序包,可能已实现了使用“Naylah”的程序包:

在您的核心/表单项目中

public class HapticFeedback
{
public static IHapticFeedback Instance { get; set; }

static HapticFeedback()
{
Instance = new DefaultHapticFeedback();
}
}

internal class DefaultHapticFeedback : IHapticFeedback
{
public void Run(HapticFeedbackType hapticFeedbackType)
{
//This is a default thing should not be used;
//throw new System.Exception("Not initialized in device platforms isbrubles");
}
}

public interface IHapticFeedback
{
void Run(HapticFeedbackType hapticFeedbackType);
}

public enum HapticFeedbackType
{
Softy,
Medium,
Heavy
}

iOS实施
public class HapticFeedbackService
{
public static void Init()
{
HapticFeedback.Instance = new iOSHapticFeedback();
}
}

public class iOSHapticFeedback : IHapticFeedback
{
public void Run(HapticFeedbackType hapticFeedbackType)
{
UIImpactFeedbackGenerator impact = null;

switch (hapticFeedbackType)
{
case HapticFeedbackType.Softy:
impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Light);
break;

case HapticFeedbackType.Medium:
impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Medium);
break;

case HapticFeedbackType.Heavy:
impact = new UIImpactFeedbackGenerator(UIImpactFeedbackStyle.Heavy);
break;
}

impact.Prepare();
impact.ImpactOccurred();
}
}

顺便说一句,您还可以像这样为触觉反馈创建一个Android实现:
public class HapticFeedbackService
{
public static void Init(Activity activity)
{
HapticFeedback.Instance = new AndroidHapticFeedback(activity);
}
}

internal class AndroidHapticFeedback : IHapticFeedback
{
private readonly Activity activity;

public AndroidHapticFeedback(Activity activity)
{
this.activity = activity;
}

public void Run(HapticFeedbackType hapticFeedbackType)
{
switch (hapticFeedbackType)
{
case HapticFeedbackType.Softy:
activity.Window.DecorView.RootView.PerformHapticFeedback(FeedbackConstants.ContextClick);
break;

case HapticFeedbackType.Medium:
activity.Window.DecorView.RootView.PerformHapticFeedback(FeedbackConstants.KeyboardPress);
break;

case HapticFeedbackType.Heavy:
activity.Window.DecorView.RootView.PerformHapticFeedback(FeedbackConstants.KeyboardPress);
break;
}
}
}

您可以查看详细的代码 here,但是如果您只是在寻找iOS实现,则可以看到 here

关于c# - Xamarin.Forms iOS依赖服务中的触觉反馈崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61413109/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com