您可以使用Caliburn.Micro(WinRT)绑定到ImageBrush的ImageSource属性吗? [英] Can you bind to the ImageSource property of an ImageBrush with Caliburn.Micro (WinRT)?

查看:66
本文介绍了您可以使用Caliburn.Micro(WinRT)绑定到ImageBrush的ImageSource属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在Caliburn.Micro中使用动态背景图像。这是我没有成功的尝试。

I was hoping to use dynamic background images with Caliburn.Micro. This is what I have tried without success.

<Grid>
 <Grid.Background>
   <ImageBrush x:Name="MyPhoto" /> 
 </Grid.Background>
</Grid>

//some view model
public class ImageViewModel
{
   public ImageSource MyPhoto {get;set;}
}

//Add Convention
//App.XAML.cs
...
public override void Configure()
{
  ...

        ConventionManager.AddElementConvention<ImageBrush>(ImageBrush.ImageSourceProperty, "ImageSource", "Loaded");
  ...
}

是否可以将ImageBrush的ImageSource与Caliburn.Micro还是有更好的方法呢?

Is it possible to bind and ImageBrush's ImageSource with Caliburn.Micro or is there a better way to do this?

推荐答案

不确定,但我认为 AddElementConvention 仅适用于 UIElement 不是 DependencyObject s。但这应该可行:

Not sure but I think AddElementConvention works only for UIElements not DependencyObjects. This should work, though:

MainPage.xaml

<Grid x:Name="MyBrush">
</Grid>

MainPageViewModel.cs

public class MainPageViewModel : Screen
{
    public MainPageViewModel()
    {
        MyPhoto = new BitmapImage(new Uri("ms-appx:///Assets/SplashScreen.png"));
    }

    public ImageSource MyPhoto { get; set; }

    public ImageBrush MyBrush
    {
        get
        {
            ImageBrush brush = new ImageBrush();
            brush.ImageSource = MyPhoto;
            return brush;
        }
    }
}

App.xaml.cs

protected override void Configure()
{
    container = new WinRTContainer();
    container.RegisterWinRTServices();

    ConventionManager.AddElementConvention<Grid>(Grid.BackgroundProperty, "Background", "Loaded");
}

或者,您也可以在XAML中手动进行绑定:

Alternatively you could do the binding by hand in XAML:

<Grid>
    <Grid.Background>
        <ImageBrush ImageSource="{Binding MyPhoto}" /> 
    </Grid.Background>
</Grid>

这篇关于您可以使用Caliburn.Micro(WinRT)绑定到ImageBrush的ImageSource属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆