.net Maui 数据绑定到 shell 弹出项 IsVisible 属性

gfm*_*ore 3 .net data-binding shell maui

所以我有一个弹出菜单,我需要使弹出项目在某些条件下消失。为了帮助我发展我的想法和理解,我有一个包含 6 个项目的弹出窗口,其中之一的标题是蓝牙。我在第一个弹出页面中创建了一个名为 ShowParameters 的按钮。我可以使视图模型中的属性为 true 或 false。这似乎工作正常。我已将蓝牙弹出项目的 IsVisible 属性绑定到此 ShowParameters 属性。这不会在单击按钮时更新 IsVisible 属性。似乎 PropertyChanged?.Invoke 未链接到 Flyout 项目,或者 Flyout 项目未链接到该属性,尽管我可以使其在 xaml 本身中出现或消失。显然,作为一个菜鸟,我做了一些非常愚蠢的事情。有人可以指出我有多愚蠢吗;)

AppShell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="TSDZ2Monitor.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:sys="clr-namespace:System;assembly=netstandard"
  
    xmlns:local="clr-namespace:TSDZ2Monitor"

    xmlns:pages="clr-namespace:TSDZ2Monitor.Pages"
  
    Shell.FlyoutBehavior="Flyout"
    FlyoutHeaderBehavior="Default"
    FlyoutVerticalScrollMode="Auto"
    FlyoutBackgroundColor="{StaticResource FlyoutBackgroundColor}">

  <Shell.BindingContext>
    <local:ShowParametersViewModel/>
  </Shell.BindingContext>

  <Shell.FlyoutHeaderTemplate>
    <DataTemplate>
      <Grid BackgroundColor="{StaticResource FlyoutBackgroundColor}"
            HeightRequest="200">
        <Image 
               HeightRequest="200"
               Source="bicycle.svg"
               Margin="10, 10, 10, 10"
               Opacity="0.6" />
        <Label Text="TSDZ2 Monitor"
               TextColor="White"
               FontAttributes="Bold" />
      </Grid>
    </DataTemplate>
  </Shell.FlyoutHeaderTemplate>

  <Shell.FlyoutFooterTemplate>
    <DataTemplate>
      <StackLayout>
        <Label Text="TSDZ2"
               TextColor="GhostWhite"
               FontAttributes="Bold"
               HorizontalOptions="Center" />
        <Label Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{0:MMMM dd, yyyy}'}"
               TextColor="GhostWhite"
               HorizontalOptions="Center" />
      </StackLayout>
    </DataTemplate>
  </Shell.FlyoutFooterTemplate>


  <Shell.ItemTemplate>
    <DataTemplate>
      <Grid ColumnDefinitions="0.2*, 0.8*">
        <Image Grid.Column="0" 
               Source="{Binding FlyoutIcon}"
               Margin="0, 20, 0, 10"
               VerticalOptions="Center"
               HeightRequest="30" />
        <Label Grid.Column="1"
               Text="{Binding Title}"
               TextColor="Yellow"
               FontSize="20"
               FontAttributes="Bold"
               VerticalTextAlignment="Center" />
      </Grid>
    </DataTemplate>
  </Shell.ItemTemplate>


  <ShellContent 
    Title="Display"
    Icon="speedometer.svg"
    ContentTemplate="{DataTemplate pages:DisplayPage}" />
  
  <ShellContent 
    Title="Bluetooth"
    Icon="bluetooth.svg"
    IsVisible="{Binding ShowParameters}"
    ContentTemplate="{DataTemplate pages:BluetoothPage}" />



  <ShellContent 
    Title="About"
    Icon="about.svg"
    ContentTemplate="{DataTemplate pages:AboutPage}" />

</Shell>
Run Code Online (Sandbox Code Playgroud)

文件夹 Pages 中的 DisplayPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             
             xmlns:local="clr-namespace:TSDZ2Monitor"
             
             x:Class="TSDZ2Monitor.Pages.DisplayPage"
             Title="Display Page">

  <ContentPage.BindingContext>
    <local:ShowParametersViewModel />
  </ContentPage.BindingContext>

  <StackLayout>
    <Label Text="Main Display"
           VerticalOptions="Center" 
           HorizontalOptions="Center" />
    
    <Button Text="Show parameters" 
            FontSize="20"
            Command="{Binding ShowParametersCommand}"/>
  </StackLayout>
</ContentPage>

Run Code Online (Sandbox Code Playgroud)

我的 ShowParametersViewModel.cs(在 ViewModels 文件夹中)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace TSDZ2Monitor
{
  //public class ShowParametersViewModel : BindableObject
  public class ShowParametersViewModel : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;

    bool showParameters = true;
    public bool ShowParameters
    {
      get { return showParameters; }
      set
      {
        if (value == showParameters) return;
        showParameters = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ShowParameters"));
      }
    }

    public ICommand ShowParametersCommand => new Command(ChangeShowParameters);

    public void ChangeShowParameters()
    {
      Console.WriteLine($"Before {ShowParameters}");
      ShowParameters = !ShowParameters;
      Console.WriteLine($"After {ShowParameters}");
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我将 AppShell.xaml 中的 Shell.BindingContext 更改为

  <Shell.BindingContext>
    <local:ShowParametersViewModel  ShowParameters="false"/>
  </Shell.BindingContext>
Run Code Online (Sandbox Code Playgroud)

它使蓝牙弹出菜单项消失,反之亦然

Col*_*SFT 5

发生问题的原因是您在页面之间操作不同的数据(视图模型),但我们需要共享数据。

下面两部分代码完全相同。

<Shell.BindingContext>
    <local:ShowParametersViewModel />
</Shell.BindingContext>
Run Code Online (Sandbox Code Playgroud)
public AppShell()
{
    InitializeComponent();
    BindingContext = new ShowParametersViewModel();
}
Run Code Online (Sandbox Code Playgroud)

实际上它创建了一个new instanceviewmodel 类,并将其设置为 BindingContext,与 DisplayPage 中的方式相同。

所以有两个不同的视图模型,你只需更改 A ,但 B 永远不会改变。

解决方案

在代码后面而不是在 xaml(DisplayPage ) 中初始化ShowParametersViewModel,以便它们共享相同的 viewmodel 。

public DisplayPage ()
{
    InitializeComponent();
    this.BindingContext = AppShell.Current.BindingContext;
}
Run Code Online (Sandbox Code Playgroud)