你能用Caliburn.Micro(WinRT)绑定到ImageBrush的ImageSource属性吗?

时间:2012-12-07 04:25:59

标签: c# xaml windows-runtime caliburn.micro

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

<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");
  ...
}

是否可以使用Caliburn.Micro绑定ImageBrush的ImageSource,或者有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

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

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中手动进行绑定:

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