0

我希望在 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");
  ...
}

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

4

1 回答 1

2

不确定,但我认为AddElementConvention仅适用于UIElements 而不是DependencyObjects。不过,这应该可行:

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>
于 2012-12-07T06:21:14.677 回答