繁体   English   中英

如何将DynamicResource的值发送到自定义控件C#后端?

[英]How can I send the value of a DynamicResource to my custom controls C# back end?

我创建了一个自定义控件:

<?xml version="1.0" encoding="UTF-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms" 
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"          
   xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
   x:Class="Ja.Templates.Button" 
   x:Name="this" 
   Test="{DynamicResource Test}" >
</Frame>

namespace Japanese.Templates
{
    public partial class Button : Frame
    {

        public static readonly BindableProperty TestProperty = 
           BindableProperty.Create(nameof(Test), typeof(string), typeof(Button), 
           default(string), propertyChanged: TestChanged);

        public string Test { get => (string)GetValue(TestProperty); 
                             set => SetValue(TestProperty, value); }

        private static void TestChanged(BindableObject bindable, 
           object oldValue, object newValue)
        {
            var x = 2;
        }

我的问题是,当我进行如下更改时:

Application.Current.Resources["Test"] = "X";
Application.Current.Resources["Test"] = "Y";

然后,该控件看不到更改,并且在我调试时它没有转到变量x。

DynamicResource值更改时,它将不会通知您的控件。 您可以在官方文档中了解有关此用途的更多信息。 我还建议您在此处阅读有关BindableProperties 信息

在共享代码示例中,您不清楚要做什么,但是,似乎应该通过将值存储在ViewModel来替换DynamicResource

暂无
暂无

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

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