C# 表单自定义控件BindingContext在DataTemplate内不工作?
我尝试将BindableLayout与StackLayout结合使用,以显示集合中的项目,但由于某些原因,使用BindableLayout时绑定不起作用。在使用XAML热重新加载进行调试时,它有时会在重新加载后开始工作,但在刷新UI后不会继续工作 回购协议是必要的,但问题代码如下: 页面XAML:(自定义控件为ComicInfo视图)C# 表单自定义控件BindingContext在DataTemplate内不工作?,c#,xaml,mvvm,xamarin.forms,C#,Xaml,Mvvm,Xamarin.forms,我尝试将BindableLayout与StackLayout结合使用,以显示集合中的项目,但由于某些原因,使用BindableLayout时绑定不起作用。在使用XAML热重新加载进行调试时,它有时会在重新加载后开始工作,但在刷新UI后不会继续工作 回购协议是必要的,但问题代码如下: 页面XAML:(自定义控件为ComicInfo视图) 10 ComicInfoView.xaml: <?xml version="1.0" encoding="UTF-8"?> <Conte
10
ComicInfoView.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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:ComicBud.Views"
mc:Ignorable="d"
x:Class="ComicBud.Views.ComicInfoView">
<ContentView.BindingContext>
<views:ComicInfoViewModel />
</ContentView.BindingContext>
<ContentView.Content>
<StackLayout Orientation="Horizontal">
<Frame WidthRequest="60"
HeightRequest="84"/>
<StackLayout RelativeLayout.XConstraint=
"{ConstraintExpression Type=RelativeToParent,
Property=Width,
Factor=0,
Constant=100}"
Margin="0, 5, 10, 0"
Spacing="0">
<Label Text="{Binding ComicName}"
HorizontalTextAlignment="Start"
FontSize="Subtitle"
LineBreakMode="WordWrap"
WidthRequest="100"/>
<Label Text="Last Read"
HorizontalTextAlignment="Start"
FontSize="Micro"
Margin="0, 5, 0, 0"/>
<Label Text="3 days ago"
HorizontalTextAlignment="Start"
FontSize="Small"/>
<Label Text="Updated"
HorizontalTextAlignment="Start"
FontSize="Micro"
Margin="0, 5, 0, 0"/>
<Label Text="2 days ago"
HorizontalTextAlignment="Start"
FontSize="Small"/>
</StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OpenComicCommand}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
</ContentView.Content>
ComicInfoViewModel.cs
using System.ComponentModel;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using FreshMvvm;
using ComicBud.Pages;
using ComicBud.Systems;
namespace ComicBud.Views
{
public class ComicInfoViewModel : INotifyPropertyChanged
{
public ComicInfoViewModel()
{
OpenComicCommand = new Command(async () => await OpenComic());
}
public event PropertyChangedEventHandler PropertyChanged;
private Comic _comic;
public Comic Comic
{
get { return _comic; }
set
{
_comic = value;
OnPropertyChanged();
OnPropertyChanged(nameof(ComicName));
}
}
public string ComicName
{
get { return "Binding Is Working"; }
}
public Command OpenComicCommand { get; }
private async Task OpenComic()
{
var navService = FreshIOC.Container.Resolve<IFreshNavigationService>(Constants.DefaultNavigationServiceName);
var page = FreshPageModelResolver.ResolvePageModel<ComicDetailPageModel>();
await navService.PushPage(page, null);
}
private void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
使用System.ComponentModel;
使用System.Threading.Tasks;
使用System.Runtime.CompilerServices;
使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
使用FreshMvvm;
使用ComicBud.page;
使用ComicBud.系统;
命名空间ComicBud.Views
{
公共类ComicInfo视图模型:INotifyPropertyChanged
{
public ComicInfo视图模型()
{
OpenComicCommand=new命令(异步()=>等待OpenComic());
}
公共事件属性更改事件处理程序属性更改;
私人漫画;
公共喜剧
{
获取{return\u comic;}
设置
{
_喜剧=价值;
OnPropertyChanged();
OnPropertyChanged(名称(ComicName));
}
}
公共字符串ComicName
{
获取{return“绑定正在工作”;}
}
公共命令OpenComicCommand{get;}
私有异步任务OpenComic()
{
var navService=FreshIOC.Container.Resolve(Constants.DefaultNavigationServiceName);
var page=FreshPageModelResolver.ResolvePageModel();
等待navService.PushPage(第页,空);
}
私有void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
编辑:在DataTemplate中用一些东西(框架、ContentView等)包装ComicInfo视图似乎可以解决这个问题。像这样:
<BindableLayout.ItemTemplate>
<DataTemplate>
<ContentView>
<views:ComicInfoView/>
</ContentView>
</DataTemplate>
</BindableLayout.ItemTemplate>
我将把这个问题标记为已解决,但如果有人能解释为什么会这样,我将不胜感激 这看起来不错,您应该将其作为问题提交给Xamarin github。您似乎使用了第三方库。您可以使用表单中的本机MVVM检查您的代码,以确认它是Xamarin.forms的问题还是MVVM库的问题。我刚刚确认了使用本机MVVM的行为是相同的。您是否可以共享您的示例,以便我可以对其进行测试。按照伊万的建议。它有一个裸露的复制拉链
<BindableLayout.ItemTemplate>
<DataTemplate>
<ContentView>
<views:ComicInfoView/>
</ContentView>
</DataTemplate>
</BindableLayout.ItemTemplate>