Search code examples
c#xamlwindows-10windows-10-mobile

How to set AppBar Height programmatically?


I'm creating a Global AppBar in App.xaml.cs that way:

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;
}

I'm implementing like this because I want this page to appear in each page of the application and the code given by Microsoft, didn't work for me, so I decided to do it as for WP 8 (Actually an adaptation since in my case, I'm using C# instead of XAML).

So, the issue that I'm facing is that the appbar takes the size of the photo and I ain't found any property to set the ImageBrush's height.

I would like to set the layout of the appbar and to share it across all the pages in the project (avoiding to copy and paste the code in each page) so any example or help would be pretty appreciated :). Thanks in advance!


Solution

  • As per you have told in Comment : "....But the first goal I wsd trying to achieve was to set a background using an imahe and resizing it."

    So, try this code : This is Sample Code:

    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;
            }
        }
    

    I have set this image with 1600X1000 resolution.

    Check this screen Shot.

    enter image description here