简体   繁体   English

Xamarin MvvmCross交叉绑定数据作为方法

[英]Xamarin MvvmCross bind data as method

I have a button, that goes back, and I'm binding it to command, that needs a parameter. 我有一个返回的按钮,并将其绑定到需要参数的命令。 Is it possible to pass command parameter as a method, that gets string? 是否可以将命令参数作为获取字符串的方法来传递?

I want to make something like that: 我想做这样的事情:

<ImageButton
    local:MvxBind="Click GoHomeCommand, CommandParameter='Application.Context.GetSharedPreferences('USER_INFO', FileCreationMode.Private).GetString('USER_FACEBOOK_ID', null)'"
    android:id="@+id/backButton"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@drawable/ic_menu_back"
    android:background="@null"
    android:paddingLeft="30dp"
    android:paddingRight="30dp" />

As you can see I binded click and I'm trying to send command parameter which now is obviously a string, but can I send a params that is the result of method: Application.Context.GetSharedPreferences('USER_INFO', FileCreationMode.Private).GetString('USER_FACEBOOK_ID', null) ?? 如您所见,我绑定了click并且试图发送命令参数,该参数现在显然是字符串,但是我可以发送作为方法结果的参数Application.Context.GetSharedPreferences('USER_INFO', FileCreationMode.Private).GetString('USER_FACEBOOK_ID', null)Application.Context.GetSharedPreferences('USER_INFO', FileCreationMode.Private).GetString('USER_FACEBOOK_ID', null) ??

This is not possible. 这是不可能的。 There are some ways to work around it. 有一些解决方法。 The easiest might be to set a ValueConverter which you implement in Android and do this in. 最简单的方法是设置一个在Android中实现并在其中实现的ValueConverter。

I think a better option is to add a function to the command, and call a interface through IoC which you implement in Android. 我认为更好的选择是在命令中添加一个函数,然后通过您在Android中实现的IoC调用接口。

There are already some plugins out there, which uses SharedPreferences internally, so you could leverage those to expose this string you have in the shared prefs. 那里已经有一些插件,这些插件在内部使用SharedPreferences,因此您可以利用它们来显示共享首选项中的此字符串。

I am the author of Cheesebaron.MvxPlugin.Settings , if you provide a configuration to the plugin, with the USER_INFO path for your settings you will be able to get the same values out of the plugin. 我是Cheesebaron.MvxPlugin.Settings的作者,如果您为插件提供配置,并使用USER_INFO路径进行设置,则可以从插件中获取相同的值。

So first add the NuGet to your Core and Droid project. 因此,首先将NuGet添加到您的Core和Droid项目中。

In the Setup.cs file of your Droid project override GetPluginConfiguration : 在Droid项目的Setup.cs文件中,覆盖GetPluginConfiguration

protected override IMvxPluginConfiguration GetPluginConfiguration(Type plugin)
{
    if (plugin == typeof(Cheesebaron.MvxPlugins.Settings.PluginLoader))
    {
        return new DroidCheeseSettingsConfiguration
        {
            SettingsFileName = "USER_INFO"
        };
    }

    return base.GetPluginConfiguration(plugin);
}

Then in your ViewModel you can expose any setting: 然后,您可以在ViewModel中公开任何设置:

public class MyViewModel : MvxViewModel
{
    private ISettings _settings;

    public MyViewModel(ISettings settings)
    {
        _settings = settings;
    }

    public string FacebookId => _settings.GetValue("USER_FACEBOOK_ID", string.Empty);
}

Now you can use FacebookId as the parameter in your binding instead. 现在,您可以将FacebookId用作绑定中的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM