繁体   English   中英

如何将 HamburgerButtonInfo 的可见性绑定到 ShellViewModel 以外的另一个视图模型?

[英]How to bind a visibility of HamburgerButtonInfo to another viewmodel other than ShellViewModel?

问题

  1. 如何通过不使用 shellviewmodel 来更改我的汉堡按钮的可见性? Ps:它是一个在 template10 上工作的 UWP 应用程序。
  2. 是否可以将汉堡按钮可见性绑定到模板 10 中的两个不同的视图模型?

Shell.xaml

xmlns:vm="using:ScanWorx.ViewModels"
xmlns:converters="using:ScanWorx.ViewModels"

<Page.DataContext>
    <vm:ShellViewModel x:Name="ViewModel" />
</Page.DataContext>

<Page.Resources>
    <converters:BooleanToVisibilityConverter x:Key="Converter" />
</Page.Resources>

<controls:HamburgerMenu x:Name="MyHamburgerMenu">
    <controls:HamburgerMenu.PrimaryButtons>
        <!--  HeatMap Generator Button  -->
        <controls:HamburgerButtonInfo  ClearHistory="True" PageType="views:HeatMapGeneratorPage" 
                                       Visibility="{x:Bind vm:LoginPageViewModel.abc, Mode=TwoWay, 
                                                    Converter={StaticResource Converter}}">
            <StackPanel Orientation="Horizontal">
                <SymbolIcon
                    Width="48"
                    Height="48"
                    Symbol="ViewAll" />
                <TextBlock
                    Margin="12,0,0,0"
                    VerticalAlignment="Center"
                    Text="HeatMap Generator" />
            </StackPanel>
        </controls:HamburgerButtonInfo>

    </controls:HamburgerMenu.PrimaryButtons>
</controls:HamburgerMenu>

LoginPageViewModel.cs

namespace ScanWorx.ViewModels
{
   class LoginPageViewModel : ViewModelBase
   {
      public static bool abc { get; set; }
      public static Visibility visibility1 { get; set; }

      public async void Button_Login_Click()
      {
         try
         {
            *code to decrypt my login details and then after decryptng i check for next condition*
            if (ApplicationData.Current.LocalSettings.Values["UserAccessLevel"].ToString() == "Administrator")
            {
                abc = true;
                visibility1 = Visibility.Visible;
            }
            else
            {
                abc = false;
                visibility1 = Visibility.Collapsed;
            }
         }
      }
   }
}

ShellViewModel.cs

  • 如果我在绑定中使用 shellviewModel 隐藏可以通过更改 - Visibility="{x:Bind vm:ShellViewModel.ShowButton} ,但是这样它只能硬编码为 false 或 true ,使其在应用程序启动时可见或折叠一次。
  • 但我不想用 shell 视图 model 更改按钮的可见性,我想在使用 LoginPageViewModel * 登录后更改它

     namespace ScanWorx.ViewModels { class ShellViewModel: ViewModelBase { public static bool ShowButton { get; set; } public ShellViewModel() { ShowButton = false; } } }

如何将 HamburgerButtonInfo 的可见性绑定到 ShellViewModel 以外的另一个视图模型?

更好的方法是进行全局设置 class 以记录是否登录。 登录成功后,将设置 class ShowButton bool 属性设置为 true。

设置.cs

public class Setting : INotifyPropertyChanged
{
    private bool _showBtn = false;
    public bool ShowBtn
    {
        get { return _showBtn; }
        set { _showBtn = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Xaml 绑定

<Application.Resources>
    <ResourceDictionary>
        <local:Setting x:Key="Setting"/>
    </ResourceDictionary>
</Application.Resources>

......

Visibility="{Binding ShowBtn, Source={StaticResource Setting}}"

价值变化

((Setting)Application.Current.Resources["Setting"]).ShowBtn = true;

暂无
暂无

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

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