2015-08-18 50 views
4

我在App.xaml.cs创造一个全球AppBar这样:如何以编程方式设置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的布局,并在项目中的所有页面上共享它(避免复制和粘贴每个页面中的代码),所以任何示例或帮助将非常感谢:)。 在此先感谢!

+0

您可以尝试[调整'BitmapImage'(http://stackoverflow.com/q/10839358/1997232)。 – Sinatr

+0

它也没有工作。我试图调整它使用[this](http://stackoverflow.com/questions/17072775/changing-the-dimensions-of-a-bitmapimage-in-wpf-and-what-kind-of-objects-can -i)也是如此,但是栏总是以相同的方式出现 –

+0

你只是试图在每个地方显示相同的图像吗? 或者你是否尝试在应用程序中为CommandBar创建视觉样式? –

回答

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

+0

感谢您的解决方案!完美的作品:) –

相关问题