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

标签 c# xamarin.android mvvmcross

我有一个在 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/

相关文章:

android - Google Play 商店(Beta 测试)中的应用程序显示 "App not compatible with your devices"

android - Mvvm交叉绑定(bind)图片文件到Android ImageView

c# - 如何使用 SQLite-Net 扩展建立 A -> B -> C 关系

c# - Fluently.Configure NHibernate 数据库从配置文件?

c# - Linq 与 Transact Sql 分组

xamarin.android - 'Realms.Realm' 的类型初始值设定项抛出异常

c# - 如何使用 EditText 字段(纯文本)的输入在服务器上编写/创建文本文件?

xamarin - Release模式下的 mvvmcross 绑定(bind)

c# - 我可以从具有多个轴的 oxyplot 图表中删除裁剪吗?

javascript - 渲染部分 View 后阻止主视图渲染