4

我以这种方式在 App.xaml.cs 中创建了一个 Global AppBar:

public static AppBar _globalAppBar = new AppBar();

public App()
{
    //Code

    SetUpBar();
}

public void SetUpBar()
{
    //SIZE
    _globalAppBar.Height = 250;

    //BACKGROUND
    ImageBrush bck = new ImageBrush();
    bck.ImageBrush = new BitmapImage(new Uri("Path"));
    _globalAppBar.Background = bck;
}

我这样做是因为我希望这个页面出现在应用程序的每个页面中,并且Microsoft给出的代码对我不起作用,所以我决定像WP 8那样做(实际上是在我的情况下,我使用的是 C# 而不是 XAML)。

所以,我面临的问题是 appbar 采用照片的大小,我没有找到任何属性来设置 ImageBrush 的高度。

我想设置 appbar 的布局并在项目中的所有页面上共享它(避免在每个页面中复制和粘贴代码),因此任何示例或帮助将不胜感激:)。提前致谢!

4

1 回答 1

2

正如您在评论中所说: “....但是我试图实现的第一个目标是使用 imahe 设置背景并调整其大小。”

所以,试试这个代码:这是示例代码:

XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Slider x:Name="slider"
                VerticalAlignment="Center"
                Maximum="250"
                Minimum="50"
                Value="100"
                ValueChanged="slider_ValueChanged"/>
</Grid>

C# :

 public static AppBar _globalAppBar = new AppBar();

    public MainPage()
    {
        this.InitializeComponent();
        SetUpBar();
    }

    public void SetUpBar()
    {
        _globalAppBar = new AppBar();
        //SIZE
        _globalAppBar.Height = 250;
        _globalAppBar.Name = "appBar";
        //BACKGROUND

        _globalAppBar.Background = new SolidColorBrush(Colors.PaleVioletRed);

        BitmapImage bmI = new BitmapImage();
        bmI= new BitmapImage(new Uri("ms-appx:///Assets/1.jpg", UriKind.RelativeOrAbsolute));

        var imageBrush = new ImageBrush();
        imageBrush.ImageSource = bmI;
        _globalAppBar.Background = imageBrush;

        AppBarButton abbtn = new AppBarButton();
        abbtn.Label = "Hello";

        _globalAppBar.Content = abbtn;
        this.TopAppBar = _globalAppBar;

    }

    private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        Slider sl = (Slider)sender;
        if (sl.Value!=0)
        {
            _globalAppBar.Height = sl.Value;
        }
    }

我已将此图像设置为 1600X1000 分辨率。

检查此屏幕截图。

在此处输入图像描述

于 2015-09-28T11:42:37.177 回答