是否可以在一行中动态创建 Xamarin 转换器?

发布于 2025-01-12 18:43:39 字数 81 浏览 0 评论 0原文

Xamarin 转换器通常必须是静态的(启动后不更改)和类,这使得它们在大多数情况下都显得有些过分了。因此我认为在运行时用一行代码定义它们会更容易。

Xamarin Converters normally have to be static (not changed after start) and classes which makes them quite an overkill for what they do in the most cases. Therefore I thought it would be easier to define them in a one-liner at runtime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2025-01-19 18:43:39

是的,如果您在调用 InitializeComponent 之前创建对象,则可以使用包装类。

这是一个示例包装类:

    public class XamarinFormsConverter<TIn, TOut> : IValueConverter {
        private readonly Func<TIn, TOut> _func;

        public XamarinFormsConverter(Func<TIn, TOut> func) => _func = func;

        public object Convert(object value, Type targetType, object _, CultureInfo __) {
            if (targetType != typeof (TOut)) {
                throw new Exception("Converter used with wrong targetType");
            }

            if (value is TIn val) {
                return _func(val);
            }
            throw new Exception("Converter used with wrong inputType");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
    }

以及如何使用它的示例:

        public ResourceDictionary Converters { get; } = new ResourceDictionary {
            ["IsLoggedInToColor"] = new XamarinFormsConverter<bool, Color>(input => input ? Color.FromRgb(132, 255, 255) : Color.FromRgb(64, 196, 255)),
            ["IsLoggedInToView"] = new XamarinFormsConverter<bool, object>(input => input ? Views.User.ToString() : Views.Login.ToString()),
            ["BoolInvert"] = new XamarinFormsConverter<bool, bool>(input => !input),
            ["FromPercent"] = new XamarinFormsConverter<int, double>(input => (double)input / 100)
        };

在初始化之前添加转换器非常重要:

        public MainPage() {
            ViewModel = new ViewModel();
            Resources.Add(ViewModel.Converters);
            InitializeComponent();
            BindingContext = ViewModel;
        }

从技术上讲,也可以在初始化后更改转换器的行为,但必须创建名称和对象前。

我希望有些人可以使用它来使他们的代码更简单、更具可读性。

Yes it is possible with an wrapper class and if you create your objects before calling InitializeComponent.

Here is an example wrapper class:

    public class XamarinFormsConverter<TIn, TOut> : IValueConverter {
        private readonly Func<TIn, TOut> _func;

        public XamarinFormsConverter(Func<TIn, TOut> func) => _func = func;

        public object Convert(object value, Type targetType, object _, CultureInfo __) {
            if (targetType != typeof (TOut)) {
                throw new Exception("Converter used with wrong targetType");
            }

            if (value is TIn val) {
                return _func(val);
            }
            throw new Exception("Converter used with wrong inputType");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
    }

and an example on how to use it:

        public ResourceDictionary Converters { get; } = new ResourceDictionary {
            ["IsLoggedInToColor"] = new XamarinFormsConverter<bool, Color>(input => input ? Color.FromRgb(132, 255, 255) : Color.FromRgb(64, 196, 255)),
            ["IsLoggedInToView"] = new XamarinFormsConverter<bool, object>(input => input ? Views.User.ToString() : Views.Login.ToString()),
            ["BoolInvert"] = new XamarinFormsConverter<bool, bool>(input => !input),
            ["FromPercent"] = new XamarinFormsConverter<int, double>(input => (double)input / 100)
        };

its important to add the Converters befor initalizing:

        public MainPage() {
            ViewModel = new ViewModel();
            Resources.Add(ViewModel.Converters);
            InitializeComponent();
            BindingContext = ViewModel;
        }

Technicallly it would also be possible to change the behaviors of the converters after the initialization, but the names and objects must be created before.

I hope some people can use this to make their code simpler and more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文