gpt4 book ai didi

c# - Caliburn 绑定(bind)异常

转载 作者:行者123 更新时间:2023-12-02 22:32:21 26 4
gpt4 key购买 nike

让我解释一下我的问题。请原谅我的长问题。开始了。

我有一个 View (BusyProviderView)

<Grid>
<xctk:BusyIndicator x:Name="aaa" IsBusy="{Binding IsRunning}" >
<xctk:BusyIndicator.BusyContentTemplate>
<DataTemplate>
<Grid cal:Bind.Model="{Binding}">
<TextBlock Name="Message"/>
</Grid>
</DataTemplate>
</xctk:BusyIndicator.BusyContentTemplate>
</xctk:BusyIndicator>
</Grid>

其中有 View 模型:

    public class BusyProviderViewModel : PropertyChangedBase, IBusyProvider
{
//two properties with INPC, Message and IsRunning
}

同样,我有一个Shell View

<Window x:Class="MvvmTest.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShellView" Height="300" Width="300">
<Grid>
<Button Height="25" x:Name="Run">Run</Button>
<ContentControl x:Name="BusyProvider"/>
</Grid>

它有一个 View 模型

public class ShellViewModel : PropertyChangedBase, IShellViewModel
{
private IBusyProvider busyProvider;

public ShellViewModel(IBusyProvider busy)
{
this.BusyProvider = busy;
}

public IEnumerable<IResult> Run()
{
yield return new DummyOperation(this.BusyProvider);
}

public IBusyProvider BusyProvider
{
get
{
return this.busyProvider;
}
set
{
if (Equals(value, this.busyProvider))
{
return;
}
this.busyProvider = value;
this.NotifyOfPropertyChange(() => this.BusyProvider);
}
}
}

DummyOperation 外观

public class DummyOperation : IResult
{
public IBusyProvider Provider { get; set; }

public DummyOperation(IBusyProvider provider)
{
Provider = provider;
}

public void Execute(ActionExecutionContext context)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (a, b) =>
{
Provider.IsRunning = true;
Provider.Message = "Working";
Thread.Sleep(TimeSpan.FromSeconds(5));
Provider.Message = "Stopping";
Thread.Sleep(TimeSpan.FromSeconds(5));
Provider.IsRunning = false;
};
worker.RunWorkerCompleted += (a, b) =>
{ Completed(this, new ResultCompletionEventArgs()); };
worker.RunWorkerAsync();

}

public event EventHandler<ResultCompletionEventArgs> Completed;
}

终于有了BootStrapper

public class AppBootstrapper : Bootstrapper<IShellViewModel>
{
private Container container;

protected override void Configure()
{
this.container = new Container();
this.container.Register<IWindowManager,WindowManager>();
this.container.Register<IShellViewModel,ShellViewModel>();
this.container.Register<IBusyProvider, BusyProviderViewModel>();
}

protected override object GetInstance(Type serviceType, string key)
{

return this.container.GetInstance(serviceType);
}


protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return this.container.GetAllInstances(serviceType);
}
protected override void BuildUp(object instance)
{
this.container.Verify();
}
}

看起来我已经设置好了一切,但是当我尝试运行它时会抛出异常。 enter image description here

我确定问题是由

引起的
 <DataTemplate>
<Grid cal:Bind.Model="{Binding}">
<TextBlock Name="Message"/>
</Grid>
</DataTemplate>

cal:Bind.Model="{Binding}

一旦我删除上面的语句,程序就会运行而不会崩溃,但不会绑定(bind)。

如果你看图片,

 protected override object GetInstance(Type serviceType, string key)
{

return this.container.GetInstance(serviceType);
}

serviceType 作为 NULL 传递,键是 "Please Wait..." ,这是从哪里来的??

最佳答案

默认情况下似乎是 Extended ToolkitBusyIndi​​cator 使用字符串 "Please Wait...." 作为 BusyContent。所以在 DataTemplate 中,DataContext 将是上面提到的字符串,这会导致 Caliburn 中的混淆和异常。

要修复它,您需要将 BusyIndi​​cator 上的 BusyContent 设置为当前的 DataContext,它将起作用:

<xctk:BusyIndicator x:Name="aaa" IsBusy="{Binding IsRunning}" 
BusyContent="{Binding}" >
<xctk:BusyIndicator.BusyContentTemplate>
<DataTemplate>
<Grid cal:Bind.Model="{Binding}">
<TextBlock Name="Message"/>
</Grid>
</DataTemplate>
</xctk:BusyIndicator.BusyContentTemplate>
</xctk:BusyIndicator>

关于c# - Caliburn 绑定(bind)异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12019483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com