我ContentPage在我的 Xamarin Forms 5 應用程式中為我的 's 使用視圖模型,并且通常從后面代碼中的方法呼叫Init()我的視圖模型中的OnAppearing()方法。
我在我的身上嘗試了同樣的方法,ContentView但它從來沒有達到這個OnAppearing()方法。
這是我的ContentView代碼:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MyApp.ViewModels"
x:Class="MyApp.MyContentView">
<ContentView.BindingContext>
<vm:MyViewModel/>
</ContentView.BindingContext>
<ContentView.Content>
<StackLayout
BackgroundColor="{StaticResource PrimaryDark }"
HeightRequest="200">
<Label
Text="{Binding User.FullName}"
TextColor="White"
FontSize="Medium"
FontAttributes="Bold"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ContentView.Content>
</ContentView>
此內容視圖的視圖模型如下所示:
public class MyViewModel : BaseViewModel
{
User user;
public MyViewModel()
{
}
public User User
{
get => user;
set
{
if (user == value)
return;
user = value;
OnPropertyChanged();
}
}
public async void Init()
{
// Get user info
var data = await _dbService.GetUser();
if(data != null)
{
User = data;
OnPropertyChanged(nameof(User));
}
}
}
在我后面的代碼中,這就是我正在做的事情:
public partial class MyContentView : ContentView
{
MyViewModel _vm;
public MyContentView()
{
InitializeComponent();
_vm = new MyViewModel();
BindingContext = _vm;
}
protected virtual void OnAppearing()
{
_vm.Init();
}
}
這種模式在我的內容頁面中運行良好,但在內容視圖中不運行。我在這里做錯了什么?
uj5u.com熱心網友回復:
內容視圖沒有像內容頁面那樣的生命周期方法。所以當內容視圖顯示或顯示在螢屏上時,開發者自定義的 OnAppearing() 和 OnDisAppearing 方法將不會被呼叫。
因此,如果您的頁面中只有一個內容視圖,您可以呼叫頁面的 OnAppearing() 方法來執行此操作。并且如果不是只有一個contentview,可以 _vm.Init();在使用contentview的實體時呼叫該方法。
uj5u.com熱心網友回復:
這是我所做的,它似乎作業正常。
首先,我創建了一個ContentView來顯示包含用戶頭像和名稱的彈出標題。請注意,我在 XAML 檔案中為此內容視圖設定了視圖模型——見下文:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:vm="clr-namespace:MyApp.ViewModels"
x:Class="MyApp.Views.FlyoutHeader">
<ContentView.BindingContext>
<vm:AppViewModel/>
</ContentView.BindingContext>
<ContentView.Content>
<StackLayout
BackgroundColor="{StaticResource PrimaryDark }"
HeightRequest="200">
<xct:AvatarView
Source="{Binding UserInfo.AvatarUrl}"
Size="100"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"/>
<Label
Text="{Binding UserInfo.FullName}"
TextColor="White"
FontSize="Medium"
FontAttributes="Bold"
HorizontalOptions="CenterAndExpand"
Margin="0,0,0,30"/>
</StackLayout>
</ContentView.Content>
</ContentView>
然后我創建了一個名為AppViewModel我打算在多個地方使用的視圖模型,包括FlyoutHeader.xaml我上面分享的那個。如下AppViewModel所示:
public class AppViewModel : BaseViewModel
{
User user { get; set; }
public AppViewModel()
{
}
public User UserInfo
{
get => user;
set
{
if (user == value)
return;
user = value;
OnPropertyChanged();
}
}
public async void Init()
{
if(user == null || user.Id == Guid.Empty)
{
var data = await _dbService.GetUser();
if(data != null)
{
UserInfo = data;
OnPropertyChanged();
}
}
}
}
最后,在 for 后面的代碼中FlyoutHeader.xaml.cs,我在建構式中呼叫Init()了視圖模型的方法:
public partial class FlyoutHeader : ContentView
{
AppViewModel _vm;
public FlyoutHeader()
{
InitializeComponent();
_vm = new AppViewModel();
_vm.Init();
BindingContext = _vm;
}
}
I'm actually a bit concerned that there maybe tight coupling with the UI and the async call being initiated in the constructor may tie up the UI thread and delay it. Please let me know if there's a better way to handle this.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446866.html
上一篇:影片針過渡
