简体   繁体   中英

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

I created a custom control:

<?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;
        }

My problem is that when I make a change like this:

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

Then the control is not seeing the change and when I debug it does not go to the variable x.

When a DynamicResource value will change it won't notify your control. You can read more about it purpose in the official documentation . I would also recommend you to read about BindableProperties here .

In the shared code example it is unclear what are you trying to do, however, it seems like you should replace DynamicResource by storing the value in a ViewModel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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