gpt4 book ai didi

c# - 如何使用 MvvmCross 5 IMvxNavigationService 获取 PendingIntent?

转载 作者:太空狗 更新时间:2023-10-29 21:59:58 24 4
gpt4 key购买 nike

我有一个在 MvvmCross 4.x 中使用的方法,该方法与 NotificationCompat.Builder 一起用于设置通知的 PendingIntent 以在通知时显示 ViewModel被用户点击。我正在尝试将此方法转换为使用 MvvmCross 5.x IMvxNavigationService 但看不到如何设置演示参数,以及如何使用新导航获取 PendingIntent API。

private PendingIntent RouteNotificationViewModelPendingIntent(int controlNumber, RouteNotificationContext notificationContext, string stopType)
{
var request = MvxViewModelRequest<RouteNotificationViewModel>.GetDefaultRequest();
request.ParameterValues = new Dictionary<string, string>
{
{ "controlNumber", controlNumber.ToString() },
{ "notificationContext", notificationContext.ToString() },
{ "stopType", stopType }
};
var translator = Mvx.Resolve<IMvxAndroidViewModelRequestTranslator>();
var intent = translator.GetIntentFor(request);
intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

return PendingIntent.GetActivity(Application.Context,
_notificationId,
intent,
PendingIntentFlags.UpdateCurrent);
}

当我点击通知时,RouteNotificationViewModel 确实出现了,但是 PrepareInitialize 没有被调用。将此方法从 MvvmCross 4.x 导航样式转换为 MvvmCross 5.x 导航样式需要什么?

最佳答案

可以在 MvvmCross 5+ 中执行此操作,但它不像以前那样干净。

对于初学者,您希望为您的事件指定 singleTop 启动模式:

[Activity(LaunchMode = LaunchMode.SingleTop, ...)]
public class MainActivity : MvxAppCompatActivity

像这样生成通知 PendingIntent:

var intent = new Intent(Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
// Putting an extra in the Intent to pass data to the MainActivity
intent.PutExtra("from_notification", true);
var pendingIntent = PendingIntent.GetActivity(Context, notificationId, intent, 0);

现在有两个地方可以处理来自 MainActivity 的这个 Intent,同时仍然允许使用 MvvmCross 导航服务:

如果在单击通知时应用未运行,则将调用 OnCreate

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if (bundle == null && Intent.HasExtra("from_notification"))
{
// The notification was clicked while the app was not running.
// Calling MvxNavigationService multiple times in a row here won't always work as expected. Use a Task.Delay(), Handler.Post(), or even an MvvmCross custom presentation hint to make it work as needed.
}
}

如果在单击通知时应用正在运行,则将调用 OnNewIntent

protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (intent.HasExtra("from_notification"))
{
// The notification was clicked while the app was already running.
// Back stack is already setup.
// Show a new fragment using MvxNavigationService.
}
}

关于c# - 如何使用 MvvmCross 5 IMvxNavigationService 获取 PendingIntent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46595077/

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