微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

设置初始值后,Shell Flyout 绑定不更新

如何解决设置初始值后,Shell Flyout 绑定不更新

我有 2 个类,一个是在所有视图模型上继承的基类。另一个是视图模型类

public class Baseviewmodel : ViewIndicatorsBase,INotifyPropertyChanged {
        private static string _userName;        
        private static string _activePlansCount;
        private static string _connectionType;
        private static string _appVersion;

        public string UserName {
            get => _userName;
            set => SetProperty(ref _userName,value);
        }

         // omitted to keep it simple
         protected bool SetProperty<T>(ref T backingStore,T value,[CallerMemberName] string propertyName = "",Action? onChanged = null) {
            if (EqualityComparer<T>.Default.Equals(backingStore,value)) {
                return false;
            }

            backingStore = value;
            onChanged?.Invoke();
            Debug.WriteLine($"Property changed -> " + value?.ToString());
            OnPropertyChanged(propertyName);
            return true;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = "") {
            PropertyChangedEventHandler? changed = PropertyChanged;
            if (changed == null) {
                return;
            }

            changed.Invoke(this,new PropertyChangedEventArgs(propertyName));
        }
}

如您所见,基类实现 INotifyPropertyChanged 并引发属性更改事件。 问题是在设置初始值后,shell 视图不会更新。如果我将一个值硬编码到绑定字符串,则一旦应用程序运行它就不会更改。我还手动检查了变量是否未正确更新(ICommand Command 中的 Flyoutviewmodel),但所有值都已正确分配,并且在此过程中也会引发事件。 但它没有在视图中更新。我在这里的另一个线程上发现了类似的问题,但它没有解决这个问题。 还要注意的是,在按下触发 Command 操作的按钮时,该操作基本上会删除绑定并重新应用它,但确实可以正常工作,并且 ui 已更新。

这是Shell类代码

public partial class ShellFlyout : Shell {

        public ShellFlyout() {
            InitializeComponent();
            BindingContext = new Flyoutviewmodel(this);
            CurrentItem = defaultItem;  
        }
    }

这是外壳视图模型

public class Flyoutviewmodel : Baseviewmodel {
        public Flyoutviewmodel(ShellFlyout appShell) {
            Title = "Navigation";           
            Command = new Command((binding) => {
                appShell.BindingContext = null;
// these display correctly
                Console.WriteLine(binding);
                Console.WriteLine((binding as Flyoutviewmodel)?.ActivePlansCount); 
                Console.WriteLine((binding as Flyoutviewmodel)?.PlanCollection?.Count);
                Console.WriteLine((binding as Flyoutviewmodel)?.UserName);
                appShell.BindingContext = new Flyoutviewmodel(appShell);

            });
            Debug.WriteLine(UserName);
        }

        private ICommand _command;

        public ICommand Command {
            get => _command;
            set => SetProperty(ref _command,value);
        }
    }

shell 视图代码

<Shell xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:xxxxxx.PCL;assembly=xxxxxx.PCL"
       xmlns:col="clr-namespace:System.Collections;assembly=netstandard"
       Opacity="0"
       Shell.NavBarHasShadow="True"
       Shell.TabBarIsVisible="False"
       Shell.ForegroundColor="White"
       FlyoutBackgroundColor="#f8f4ff"
       FlyoutVerticalScrollMode="Auto"
       Shell.TitleColor="White"
       Routing.Route="home"
       BackgroundColor="#8E2DE2"
       FlyoutHeaderBehavior="Fixed"    
       x:Class="xxxxxx.PCL.ShellFlyout">    
    <Shell.ItemTemplate>
        <DataTemplate >
            <controls:FlyoutItemTemplate />
        </DataTemplate>     
    </Shell.ItemTemplate>

    <Shell.FlyoutHeaderTemplate>
        <DataTemplate>
            <ContentView>
                <StackLayout BackgroundColor="#f8f4ff"
                             Padding="0">
                    <Frame Padding="5"
                           CornerRadius="6"
                           HasShadow="True"
                           Margin="10,10,0"
                           BackgroundColor="#f8f4ff">
                        
                        <Label>
                            <Label.FormattedText>
                                <FormattedString>
                                    <Span Text="test"
                                          FontSize="40"
                                          FontFamily="Ubuntu-Bold"
                                          TextColor="Black"
                                          FontAttributes="Bold" />
                                    <Span Text="tesstttt"
                                          FontSize="35"
                                          TextColor="Black"
                                          FontFamily="Ubuntu-Bold"
                                          FontAttributes="Bold" />
                                </FormattedString>
                            </Label.FormattedText>
                        </Label>
                    </Frame>

                    <Button Command="{Binding Command}"
                            CommandParameter="{Binding}" />
                    <Label Text="{Binding ActivePlansCount,StringFormat='{0} PLANS ACTIVE'}"
                           FontAttributes="Bold"
                           FontSize="13"
                           TextColor="Black"
                           TextTransform="Uppercase"
                           HorizontalOptions="CenterandExpand" />
                    <Label Text="{Binding ConnectionType}"
                           TextTransform="Uppercase"
                           FontAttributes="Bold"
                           FontSize="13"
                           TextColor="Black"
                           HorizontalOptions="CenterandExpand" />
                    <Label Text="{Binding UserName}"
                           TextTransform="Uppercase"
                           FontAttributes="Bold"
                           FontSize="13"
                           TextColor="Black"
                           HorizontalOptions="CenterandExpand" />
                </StackLayout>
            </ContentView>
        </DataTemplate>
    </Shell.FlyoutHeaderTemplate>
</Shell>

**

更新

** 由于我无法理解这背后的问题,我目前正在做一个解决方法,在加载值后设置绑定视图模型,以便初始值将是正确的,因为这些值不会每次都改变并且在初始化后的 UI。

获取后执行此操作。

Shell.Current.BindingContext = null;
Shell.Current.BindingContext = new Flyoutviewmodel();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。