FlyoutFooter ContentView在.NET MAUI应用中注入依赖项
我在.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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是想出了一种做到这一点的方法。我给AppShell一个视图模型而不是使用视图模型的控件,因此我可以使用FlyoutFooterTemplate。
我将IserviceProvider注入AppShell
,然后将bindingContext设置为我想要的视图模型
,然后将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.
I inject IServiceProvider into AppShell
and then set the BindingContext to the view model I want
As I type this out, I am going inject the viewmodel into the constructor instead.