我在MvvmCross和Xamarin Forms中绑定时遇到问题。当我从MainViewModel导航到FileInfoViewModel时,出现以下错误:
(MvvmCross.Logging.MvxLog)找不到候选视图RootActivity的视图模型关联
我使用了here中所述的MvvmCross设置
这是我在Android项目中的RootActivity.cs代码:
[Activity(Label = "XFCubage", Icon = "@mipmap/icon", Theme = "@style/MainTheme",
MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class RootActivity : MvxFormsAppCompatActivity<MvxFormsAndroidSetup<Core.App, App>, Core.App, App>
{
protected override void OnCreate(Bundle savedInstanceState)
{
CrossCurrentActivity.Current.Activity = this;
base.OnCreate(savedInstanceState);
}
}
这是我从MainViewModel导航的FileInfoViewModel.cs(在导航过程中,我正在传递对象InfoDetail):
namespace XFCubage.Core.ViewModels
{
public class FileInfoViewModel : MvxViewModel<InfoDetail>
{
public ObservableCollection<SideMenuItem> MenuItems { get; set; }
public InfoDetail Detail { get; private set; }
public SideMenuItem SelectedMenuItem { get; set; }
private int _sideMenuWidth;
public int SideMenuWidth
{
get => _sideMenuWidth;
set => SetProperty(ref _sideMenuWidth, value);
}
private IMvxNavigationService _navigationService;
public FileInfoViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
InitializeSideMenuItems();
CrossDeviceOrientation.Current.OrientationChanged += (sender,
args) =>
{
var orientation = args.Orientation;
SetSideMenuWidth(orientation);
};
}
void SetSideMenuWidth(DeviceOrientations orientation)
{
if (orientation == DeviceOrientations.Landscape ||
orientation == DeviceOrientations.LandscapeFlipped)
SideMenuWidth = 250;
else
SideMenuWidth = 65;
}
public void InitializeSideMenuItems()
{
MenuItems = new ObservableCollection<SideMenuItem>();
MenuItems.Add(new SideMenuItem() { Id = 1, Title = "FILE INFO", ImageSource = ImageSource.FromFile("fileinfo.png"), Selected = true });
MenuItems.Add(new SideMenuItem() { Id = 2, Title = "LOADING ADDRESS", ImageSource = ImageSource.FromFile("loadingaddress.png"), Selected = false });
MenuItems.Add(new SideMenuItem() { Id = 3, Title = "DELIVERY ADDRESS", ImageSource = ImageSource.FromFile("deliveryaddress.png"), Selected = false });
MenuItems.Add(new SideMenuItem() { Id = 4, Title = "QUOTE ADDRESS", ImageSource = ImageSource.FromFile("quoteaddress.png"), Selected = false });
MenuItems.Add(new SideMenuItem() { Id = 5, Title = "BILLING ADDRESS", ImageSource = ImageSource.FromFile("wallet.png"), Selected = false });
MenuItems.Add(new SideMenuItem() { Id = 6, Title = "ROOMS", ImageSource = ImageSource.FromFile("rooms.png"), Selected = false });
MenuItems.Add(new SideMenuItem() { Id = 7, Title = "SUMMARY", ImageSource = ImageSource.FromFile("summary.png"), Selected = false });
SelectedMenuItem = MenuItems[0];
}
public override void Prepare(InfoDetail parameter)
{
Detail = parameter;
}
}
}
这是我的FileInfoPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<views:MvxContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:components="clr-namespace:XFCubage.Core.Components;assembly=XFCubage.Core"
xmlns:viewModels="clr-namespace:XFCubage.Core.ViewModels;assembly=XFCubage.Core"
x:TypeArguments="viewModels:FileInfoViewModel"
x:Class="XFCubage.Views.FileInfoPage"
BackgroundColor="{StaticResource PageBackgroundColor}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding SideMenuWidth}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<listview:SfListView ItemsSource="{Binding MenuItems}"
BackgroundColor="{StaticResource TableHeaderBackground}"
ItemSpacing="0,10,0,10"
SelectionMode="Single" SelectionGesture="Tap" SelectedItem="{Binding SelectedMenuItem, Mode=TwoWay}">
<listview:SfListView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal">
<BoxView WidthRequest="3"/>
<Image Source="{Binding ImageSource}" WidthRequest="24" HeightRequest="24" Margin="10,0,20,0"/>
<Label Text="{Binding Title}" TextColor="{StaticResource TextColor}" VerticalTextAlignment="Center"/>
</StackLayout>
</DataTemplate>
</listview:SfListView.ItemTemplate>
<listview:SfListView.SelectedItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" BackgroundColor="{StaticResource SelectedItemBackgroundColor}">
<BoxView WidthRequest="3" BackgroundColor="{StaticResource HighlightColor}"/>
<Image Source="{Binding ImageSource}" WidthRequest="24" HeightRequest="24" Margin="10,0,20,0"/>
<Label Text="{Binding Title}" TextColor="{StaticResource TextColor}" VerticalTextAlignment="Center"/>
</StackLayout>
</DataTemplate>
</listview:SfListView.SelectedItemTemplate>
</listview:SfListView>
</Grid>
问题出现在XAML bindin的这一行中。我收到错误消息:
(MvvmCross.Logging.MvxLog)未找到候选视图RootActivity的视图模型关联:
<ColumnDefinition Width="{Binding SideMenuWidth}"/>
当我将此行更改为:
<ColumnDefinition Width="250"/>
一切似乎都还好。奇怪的是,对列表视图(我从MainViewModel传递到FileInfoViewModel的模型 Detail )的绑定有效。
因为我很困惑,请您能帮助我。
答案 0 :(得分:0)
根据您的代码,我看不到将(数据)ViewModel链接到(UI)FileInfoPagelian的位置,我想这是您收到此错误消息的原因。
通常,我创建Xamarin.Form,UI在xxx.xaml中,并在xxx.xaml.cs中连接Viewmodel,如下所示:
https://www.c-sharpcorner.com/article/xamarin-forms-create-mvvm-data-binding-application/