FlyoutFooter ContentView在.NET MAUI应用中注入依赖项

发布于 2025-02-05 20:59:31 字数 3313 浏览 1 评论 0原文

我在.NET MAUI应用程序中为FlyoutFooter创建了一个contentView。在页脚中,我将imageButton放置在用户可以使用的图标上。

我还在整个应用程序中都使用依赖项注入,因此我在DI容器中同时注册了flyoutfooter视图及其相应的flyoutfooterviewModel

问题是当我尝试告诉shell使用flyoutfooter内容视图时,它不喜欢它,因为它的构造函数会收到一个参数 - 请参见下面的屏幕截图。

flyoutfooter的代码看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:MyApp.ViewModels.Flyout"
             x:Class="MyApp.Views.Flyout.FlyoutFooter"
             x:DataType="vm:FlyoutFooterViewModel">
    <Grid
      RowDefinitions="120"
      ColumnDefinitions="150, 150">
        <Image
            Grid.Row="0"
            Grid.Column="0"
            HorizontalOptions="StartAndExpand"
            Margin="50,0,0,0">
            <Image.Source>
                <FontImageSource
                    FontFamily="MISHRP"
                    Glyph="{StaticResource SettingsIcon}"
                    Color="White"/>
            </Image.Source>
            <Image.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding GotoSettingsCommand}" />
            </Image.GestureRecognizers>
        </Image>
        <Image
            Grid.Row="0"
            Grid.Column="1"
            HorizontalOptions="EndAndExpand"
            Margin="0,0,30,0">
            <Image.Source>
                <FontImageSource
                    FontFamily="MISHRP"
                    Glyph="{StaticResource PowerIcon}"
                    Color="White"/>
            </Image.Source>
            <Image.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding SignOffCommand}"/>
            </Image.GestureRecognizers>
        </Image>
    </Grid>
</ContentView>

fly> flyoutfooter.xaml背后的代码中,我注入视图模型 - :

public partial class FlyoutFooter : ContentView
{

    FlyoutFooterViewModel _vm;
    public FlyoutFooter(FlyoutFooterViewModel vm)
    {
        InitializeComponent();
        _vm = vm;
        BindingContext = _vm;
    }
}

视图模型看起来像这样:

public class FlyoutFooterViewModel : BaseViewModel
{
   IDatabaseService _dbService;

   public ICommand GotoSettingsCommand { get; }
   public ICommand SignOffCommand { get; }

   public FlyoutFooterViewModel(IDatabaseService dbService)
   {
      _dbService = dbService;

      GotoSettingsCommand = new AsyncRelayCommand(GotoSettings);
      SignOffCommand = new AsyncRelayCommand(SignOff);
   }

   async Task GotoSettings()
   {
      // Send user to Settings page
   }

   async Task SignOff()
   {
      SecureStorage.Remove("access_token");

      await _dbService.ClearAll();

      await Shell.Current.GoToAsync($"///{nameof(Login)}");
   }
}

处理这种情况的任何建议?

I created a ContentView for the FlyoutFooter in my .NET MAUI app. In the footer, I placed an ImageButton with an icon that the user can use to sign out.

I'm also using dependency injection throughout the app so I registered both the FlyoutFooter view and its corresponding FlyoutFooterViewModel in the DI container.

The problem is when I try to tell the Shell to use the FlyoutFooter content view, it doesn't like it becuase its constructor receives a parameter -- see screen shot below.

enter image description here

The code behind for the FlyoutFooter looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:MyApp.ViewModels.Flyout"
             x:Class="MyApp.Views.Flyout.FlyoutFooter"
             x:DataType="vm:FlyoutFooterViewModel">
    <Grid
      RowDefinitions="120"
      ColumnDefinitions="150, 150">
        <Image
            Grid.Row="0"
            Grid.Column="0"
            HorizontalOptions="StartAndExpand"
            Margin="50,0,0,0">
            <Image.Source>
                <FontImageSource
                    FontFamily="MISHRP"
                    Glyph="{StaticResource SettingsIcon}"
                    Color="White"/>
            </Image.Source>
            <Image.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding GotoSettingsCommand}" />
            </Image.GestureRecognizers>
        </Image>
        <Image
            Grid.Row="0"
            Grid.Column="1"
            HorizontalOptions="EndAndExpand"
            Margin="0,0,30,0">
            <Image.Source>
                <FontImageSource
                    FontFamily="MISHRP"
                    Glyph="{StaticResource PowerIcon}"
                    Color="White"/>
            </Image.Source>
            <Image.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding SignOffCommand}"/>
            </Image.GestureRecognizers>
        </Image>
    </Grid>
</ContentView>

In the code behind for FlyoutFooter.xaml, I inject the view model -- see below:

public partial class FlyoutFooter : ContentView
{

    FlyoutFooterViewModel _vm;
    public FlyoutFooter(FlyoutFooterViewModel vm)
    {
        InitializeComponent();
        _vm = vm;
        BindingContext = _vm;
    }
}

The view model looks like this:

public class FlyoutFooterViewModel : BaseViewModel
{
   IDatabaseService _dbService;

   public ICommand GotoSettingsCommand { get; }
   public ICommand SignOffCommand { get; }

   public FlyoutFooterViewModel(IDatabaseService dbService)
   {
      _dbService = dbService;

      GotoSettingsCommand = new AsyncRelayCommand(GotoSettings);
      SignOffCommand = new AsyncRelayCommand(SignOff);
   }

   async Task GotoSettings()
   {
      // Send user to Settings page
   }

   async Task SignOff()
   {
      SecureStorage.Remove("access_token");

      await _dbService.ClearAll();

      await Shell.Current.GoToAsync(
quot;///{nameof(Login)}");
   }
}

Any suggestions for handling this scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2025-02-12 20:59:31

我只是想出了一种做到这一点的方法。我给AppShell一个视图模型而不是使用视图模型的控件,因此我可以使用FlyoutFooterTemplate。

<Shell.FlyoutFooterTemplate>
    <DataTemplate>
        <Label Text="{Binding DABUser.Name}" BackgroundColor="Red"/>
    </DataTemplate>
</Shell.FlyoutFooterTemplate>

我将IserviceProvider注入AppShell

public AppShell(IServiceProvider serviceProvider)

,然后将bindingContext设置为我想要的视图模型

BindingContext = serviceProvider.GetService<FlyoutFtViewModel>()

,然后将ViewModel注入构造函数。

I just figured out a way to do this. Instead of the control with a view model, I gave AppShell a view model so I can use FlyoutFooterTemplate.

<Shell.FlyoutFooterTemplate>
    <DataTemplate>
        <Label Text="{Binding DABUser.Name}" BackgroundColor="Red"/>
    </DataTemplate>
</Shell.FlyoutFooterTemplate>

I inject IServiceProvider into AppShell

public AppShell(IServiceProvider serviceProvider)

and then set the BindingContext to the view model I want

BindingContext = serviceProvider.GetService<FlyoutFtViewModel>()

As I type this out, I am going inject the viewmodel into the constructor instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文