Caliburn绑定异常

cry*_*ted 2 c# wpftoolkit caliburn.micro simple-injector

让我解释一下我的问题.请原谅我这个长期的问题.在这里.

我有一个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>
Run Code Online (Sandbox Code Playgroud)

哪个有View型号:

    public class BusyProviderViewModel : PropertyChangedBase, IBusyProvider
{
//two properties with INPC, Message and IsRunning
}
Run Code Online (Sandbox Code Playgroud)

我再次有一个Shell视图

<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>
Run Code Online (Sandbox Code Playgroud)

其中有一个视图模型

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

最后我有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();
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来我已经设置了一切,但是当我尝试运行它时抛出异常. 在此输入图像描述

我确信这个问题是由问题造成的

 <DataTemplate>
            <Grid cal:Bind.Model="{Binding}">
                <TextBlock Name="Message"/>
            </Grid>
        </DataTemplate>
Run Code Online (Sandbox Code Playgroud)

CAL:Bind.Model ="{}绑定

一旦我删除上述语句,程序运行没有崩溃但没有绑定.

如果你看看图像,

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

        return this.container.GetInstance(serviceType);
    }
Run Code Online (Sandbox Code Playgroud)

serviceType作为NULL传递,键是"Please Wait ....",它来自哪里?

nem*_*esv 5

这似乎默认情况下,扩展工具包BusyIndicator使用字符串"Please Wait...."BusyContent.所以里面DataTemplateDataContext将是上面提到的字符串,这将导致在卡利的混乱和异常.

要解决此问题,您需要将BusyContenton设置BusyIndicator为当前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>
Run Code Online (Sandbox Code Playgroud)