如何通过编程方式设置AppBar的高度?

4

我正在App.xaml.cs中创建一个全局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)。
所以,我面临的问题是应用栏占用了照片的大小,而我没有找到任何属性来设置ImageBrush的高度。
我希望设置应用栏的布局,并在项目中的所有页面共享它(避免在每个页面中复制和粘贴代码),因此任何示例或帮助都将非常感激 :). 预先感谢!

你可以尝试调整BitmapImage的大小。 - Sinatr
它仍然没有起作用。我也尝试使用这个来调整大小,但是进度条总是以同样的方式出现。 - Eva FP
你只是想在每个地方显示相同的图像吗?还是你想为整个应用程序创建一个命令栏的视觉样式? - Adam Kinney
实际上不是这样的,这只是一个简单的例子。我想要一个带有两个按钮的应用栏:一个带有下拉菜单和搜索按钮;以及一个标志。但我最初尝试实现的第一个目标是使用图像设置背景并调整其大小。 - Eva FP
1个回答

2

根据您在评论中提到的内容:

"....但我一开始想实现的第一个目标是使用图片设置背景并调整大小。"

因此,请尝试使用以下代码:这是示例代码:

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分辨率。
请查看此屏幕截图。 enter image description here

谢谢解决方案!!完美运行 :) - Eva FP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接