Changing the buttons in the Applicationbar? - c#

I want to change the icons and what they do in code in my WP7 application. I've tried in the MainPage constructor and in the MainPage_Loaded but it says ApplicationBar (x:Name) is null all the time.
How can I change it since I use same page and different states?

Unfortunately the application bar buttons are not accessible via code at the moment. I followed one of the examples of the official Mircosoft Training Kit for WP7 to accomplish that task. Here is the XAML and some code:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar>
<shell:ApplicationBarIconButton IconUri="/icons/appbar.pin.png" IsEnabled="True" Text="Anpinnen" x:Name="appPinPage" Click="appPinPage_Click" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
Please note that ApplicationBar is a property of PhoneApplicationPage so that you do not have to give an explicit name to the ApplicationBar object that you assign to the ApplicationBar property of the PhoneApplicationPage. Here is an example of changing the picture of the button in the xaml code above. That code is called in the overridden OnNavigatedTo() method of the PhoneApplicationPage.
if (this.ViewModel.IsPinned())
{
((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).Text = Resource1.txtUnpin;
((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.unpin.png", UriKind.Relative);
}
else
{
((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).Text = Resource1.txtPin;
((ApplicationBarIconButton)this.ApplicationBar.Buttons[0]).IconUri = new Uri("/icons/appbar.pin.png", UriKind.Relative);
}

Try removing the x:Name property and access it using Page.ApplicationBar.

Unfortunately, the ApplicationBar isn't a Silverlight control (doesn't inherit from UIElement); what that means is that it can't be accessed with a x:Name neither the buttons inside it, but you can access it inside the page using the ApplicationBar property!
Check this sample to see how you can create and access the ApplicationBar from the code behind of a page.
If you are willing to go with an MVVM architecture, you should check the ApplicationBarBehaviour from the Cimbalino Windows Phone Toolkit, it will save you a lot of work!!!

Related

Hide Navigation Bar Prism Xamarin

I have the following set in my view code behind
NavigationPage.SetHasNavigationBar(this, false);
and I have it also in the XAML
NavigationPage.HasNavigationBar="False"
However the navigation panel is still displaying when navigating with Prism and the follow code:
await NavigationService.NavigateAsync("NavigationPage/RecipeListPage");
Navigating with the absolute method such as:
NavigationService.NavigateAsync(new System.Uri("http://www.RecipeDatabase/RecipeListPage", System.UriKind.Absolute));
Fixes this issue. Not sure why its needed. Also hiding with just the XAML code as well is sufficient.
Enjoy.

How can I easily hide all transport controls in UWP MediaPlayerElement except a few?

I have a MediaPlayerElement that plays a video automatically and I want the user to be only able to seek in the video and press pause/stop/play.
It looks like I have to set AreTransportControlsEnabled to true and then hide all the controls I don't want one by one as per default all controls are visible.
So I did this:
<MediaPlayerElement x:Name="mediaPlayer" AreTransportControlsEnabled="True">
<MediaPlayerElement.TransportControls>
<MediaTransportControls
ShowAndHideAutomatically="True"
IsFullWindowButtonVisible="False"
IsNextTrackButtonVisible="False"
IsPreviousTrackButtonVisible="False"
IsVolumeButtonVisible="False"
IsZoomButtonVisible="False"
IsFastForwardButtonVisible="False"
IsFastRewindButtonVisible="False"
IsPlaybackRateButtonVisible="False"
IsRepeatButtonVisible="False"
IsSkipBackwardButtonVisible="False"
IsSkipForwardButtonVisible="False"
Windows10version1803:IsCompactOverlayButtonVisible="False"
IsSeekBarVisible="True"
IsSeekEnabled="True"
IsStopButtonVisible="True"
/>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
For my taste this looks really cumbersome. Isn't there are setting like "hideall=true" and then I could only enable those I want. And for example, there seems to be no way to also hide the "cast to device" button, so with the current approach the user would always see this button, what I don't really like:
Any ideas?
Removing "CastButton" from generic.xaml didn't work out for me. I found a solution for removing Cast to Device button in another forum :
https://social.msdn.microsoft.com/Forums/windows/en-US/e3307864-f194-4197-9f0d-bb2b8cd7228c/uwp-custom-media-transport-controls-hide-custom-buttons
Here is the working code for removing "CastButton" AppBarButton from Mediaplayer at runtime.
public class CustomMediaTransportControls: MediaTransportControls
{
protected override void OnApplyTemplate()
{
AppBarButton CastButton = GetTemplateChild("CastButton") as AppBarButton;
var MediaControlsCommandBar = GetTemplateChild("MediaControlsCommandBar") as CommandBar;
MediaControlsCommandBar.PrimaryCommands.Remove(CastButton);
base.OnApplyTemplate();
}
}
You can create your own media transport controls by setting AreTransportControlsEnabled to false, and using the Play and Pause methods on MediaPlayer.
For more info and examples, see Create custom transport controls.
The official Media transport controls sample will be a good start.
For example, in the official sample, if you do not want to show the 'cast to device' button, you could directly remove the AppBarButton named as CastButton in the generic.xaml.

Manually trigger NavigationThemeTransition in Windows Phone WinRT/Universal app

In my application I use a ContentControl like this:
<ContentControl x:Name="Content">
<ContentControl.ContentTransitions>
<NavigationThemeTransition />
</ContentControl.ContentTransitions>
</ContentControl>
The problem is that the NavigationThemeTransition is not triggered when changing the Content property of the ContentControl. I think this is because it is only triggered in a Frame control when calling the Navigate() method.
I need this transition to be run when the Content of the ContentControl changes...
Is there a way to trigger the navigation-in and navigation-out animation manually?
Or is there some visual state to which the control can got to run the animation?
To achieve this You would be needed to have Custom Transition for the Control shown here custom transitions and for implementing Transition Effects in coding you can have Reference from here Using Page Transitions via Code
You could try to use a Frame control instead of a ContentControl. If that doesn't work - you'd need to create a custom control that has a Frame in its template and when its content change happens - it would navigate to a new page to display the new content.
I haven't seen a way to trigger the built-in transitions other than invoking an action that these transitions were created for. Personally - I would rather create my own transition than hack around to invoke the built-in one. You should be able to create one that looks exactly the same as the built-in one.

Change image from other classes in windows phone

I'm developing some apps for windows phone using c# but I have some troubles trying to pass some data (textblocks, images etc) from the MainPage to other classes.
I can explain it better saying:
I have MainPage.xaml + MainPage.xaml.cs (where my image is called "myImage"). This is the xaml code:
<Image Name="myImage" DataContext="{Binding}" />
and using buttons in this class I can change the image simply typing:
myImage.Source = new BitmapImage(new Uri("images/xxxx.png", UriKind.Relative));
and it works.
I have an another class "x.cs" and I want to change the image when something happends, but how can I can access myImage from this class and change its Source without using timers? I've searched a lot without finding interesting things...
There are couple of way to do the same.
1). Event - Raise event - Subscribe event mechanism.
2). Use Event Aggregator instead of traditional event delegation model.
3). Use INotifyPropertyChange or similar object to get notification on some change and do what is intented to be done.
None of them would be hard to understand, find, learn and implement.

instantiate navigationservice class

I was working on a WPF project today that had a main nav window and then 4 pages that were loaded within this main window (using NavigationService.Nagivate...).. Within the XAML this created a lot of duplicate code so I wanted to refactor the menu into a user control that I could then bind to each page. I tried to create a class to handle the navigation and loading of each page but I discovered that NavigationServices is a sealed class and cannot be instantiated.
Would anyone please provide a suggestion/solution on how to create a usercontrol with a menu item that will allow the ability to navigate to new pages within the project. I have been able to do this within pages and the direct code behind but I have not had any luck trying to separate the two. If this is too vague please let me know and I will provide more deails with code samples.
Thanks in advance
Create a NavigationService dependency property on your user control. Then, when you instantiate your control, bind this property to the NavigationService of the container where the navigation should occur. For example, a page might display the user control like this:
<local:NavBox NavigationService="{Binding NavigationService, RelativeSource={RelativeSource AncestorType={x:Type NavigationWindow}}}" />
Now when your UserControl calls Navigate on its NavigationService, that will effectively call Navigate on the containing NavigationWindow's NavigationService. (This can be modified in the obvious way to support Frame or Page instead of NavigationWindow.)

Categories