Event Handler returning null c#? - c#

I am implementing a swipe cards view in Xamarin.Android. I am firing an event from RatingCardAdapter to reset the swipe cards view after all the cards have been swiped. For the first time, the event is not null and the swipe cards get reset but on the second try, the event handler returns null. As a result, I cannot set the value of shouldResetSwipe. How can i solve this problem?
Adapter
public class RatingCardAdapter : BaseCardAdapter
{
private Context context;
public event EventHandler OnLastCardSwiped;
public RatingCardAdapter(Context context, SwipeCardsView SwipeView)
{
this.context = context;
this.SwipeView = SwipeView;
SwipeView.SetCardsSlideListener(this);
}
public void OnCardVanish(int p0, SwipeCardsView.SlideType p1)
{
if (p0 == (Count - 1)) // p0 becomes 4 when last card is swiped
{
if (OnLastCardSwiped != null) //becomes null when rating adapter called 2nd time
OnLastCardSwiped(this, new OnLastCardSwipeArgs
{
shouldResetSwipe = true });
}
}
public class OnLastCardSwipeArgs : EventArgs
{
public bool shouldResetSwipe { get; set; }
}
Activity
private SwipeCardsView swipeCardsView;
RatingCardAdapter ratingCardAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_rating_session);
swipeCardsView = FindViewById<SwipeCardsView>
(Resource.Id.swipeCardsRating);
swipeCardsView.RetainLastCard(false);
swipeCardsView.EnableSwipe(true);
setSwipeData();
}
void setSwipeData() {
ratingCardAdapter = new RatingCardAdapter(this, swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
ratingCardAdapter.OnLastCardSwiped += (sender, e) =>
{
if (e.shouldResetSwipe)
{
Console.WriteLine("restart set " + e.shouldResetSwipe);
restartSwipeCard();
}};
}
void restartSwipeCard()
{
Console.WriteLine("restartswipe");
ratingCardAdapter = new RatingCardAdapter(this,swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
}

Since you are creating a new instance of RatingCardAdapter in restartSwipeCard method, you need to subscribe too it's event also, since its tied to RatingCardAdapter intance. So move your lambda method to intance method to prevent code duplication and do same subscription inside restartSwipeCard method:
void restartSwipeCard()
{
Console.WriteLine("restartswipe");
ratingCardAdapter = new RatingCardAdapter(this,swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
// need this subscription here too
ratingCardAdapter.OnLastCardSwiped += (sender, e) =>
{
if (e.shouldResetSwipe)
{
Console.WriteLine("restart set " + e.shouldResetSwipe);
restartSwipeCard();
}};
}
Or even better rename setSwipeData to initSwipeData and update code like this:
private SwipeCardsView swipeCardsView;
RatingCardAdapter ratingCardAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_rating_session);
swipeCardsView = FindViewById<SwipeCardsView>
(Resource.Id.swipeCardsRating);
swipeCardsView.RetainLastCard(false);
swipeCardsView.EnableSwipe(true);
initSwipeData();
}
private void initSwipeData()
{
ratingCardAdapter = new RatingCardAdapter(this, swipeCardsView);
swipeCardsView.SetAdapter(ratingCardAdapter);
ratingCardAdapter.OnLastCardSwiped += (sender, e) =>
{
if (e.shouldResetSwipe)
{
Console.WriteLine("restart set " + e.shouldResetSwipe);
Console.WriteLine("restartswipe");
initSwipeData();
}};
}

Related

Xamarin avplayercontroller custom controls

I am trying to add some controls to a sample from https://github.com/pro777s/Xam.Forms.VideoPlayer
Modifying these codes from here. I have a created a custom controller.
Not sure how I am suppose to do it. Help will be appreciated. I need to add two buttons, Previous and Next to play and next and previous track.
public class CustomButton : UIButton
{
public CustomButton() : base(UIButtonType.System)
{ }
public int someValue;
protected override void Dispose(bool disposing)
{
Console.WriteLine("Disposed button");
base.Dispose(disposing);
}
~CustomButton()
{
Console.WriteLine("Finalized button");
}
}
public class CustomPlayerController : AVPlayerViewController
{
private AVPlayer _Player;
private AVAudioUnitEQ aVAudioUnitEQ;
// IMusicManager musicManager;
static CustomPlayerController playerController;
public static CustomPlayerController GetInstance(AVPlayer player)
{
if (playerController == null)
{
playerController = new CustomPlayerController(player);
}
//DependencyService.RegisterSingleton<IBackGroundPlayer>(playerController);
return playerController;
}
private CustomPlayerController(AVPlayer player)
{
_Player = player;
}
private void buttonTapped(object sender, EventArgs e)
{
//print("button was tapped")
// replay/comment logic here
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var customView = this.View.Subviews[0] as CustomButton;
Console.WriteLine("Magic number: " + customView.someValue);
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var btn = new CustomButton()
{
Frame = new CoreGraphics.CGRect(0, 20, 320, 40),
someValue = 4711
};
btn.SetTitle("Click me", UIControlState.Normal);
btn.TouchUpInside += (sender, e) => {
//btn.RemoveFromSuperview();
};
this.Add(btn);
}
}
This is the renderer, i have changed this little bit.
AVPlayer player;
AVPlayerItem playerItem;
CustomPlayerController _playerViewController; // solely for ViewController property
NSObject playCompleteNotification, playerItemFailedToPlayToEndTimeNotification;
NSObject playbackBufferEmptyObserver, playbackLikelyToKeepUpObserver, playbackBufferFullObserver;
public override UIViewController ViewController => _playerViewController;
protected override void OnElementChanged(ElementChangedEventArgs<VideoPlayer> args)
{
base.OnElementChanged(args);
if (args.NewElement != null)
{
if (Control == null)
{
// Create AVPlayerViewController
// Set Player property to AVPlayer
player = new AVPlayer();
_playerViewController = CustomPlayerController.GetInstance (player);
_playerViewController.Player = player;
// End of play notification
playCompleteNotification = NSNotificationCenter.DefaultCenter.AddObserver(
AVPlayerItem.DidPlayToEndTimeNotification,
OnAVPlayerItemDidPlayToEndTime, player.CurrentItem);
// Play error occured notification
playerItemFailedToPlayToEndTimeNotification =
AVPlayerItem.Notifications.ObserveItemFailedToPlayToEndTime(OnAVPlayerItemFailedToPlayToEndTime);
// Use the View from the controller as the native control
SetNativeControl(_playerViewController.View);
}
SetAreTransportControlsEnabled();
SetSource();
args.NewElement.UpdateStatus += OnUpdateStatus;
args.NewElement.PlayRequested += OnPlayRequested;
args.NewElement.PauseRequested += OnPauseRequested;
args.NewElement.StopRequested += OnStopRequested;
args.NewElement.ShowTransportControlsRequested += OnShowTransportControls;
args.NewElement.HideTransportControlsRequested += OnHideTransportControls;
}
if (args.OldElement != null)
{
args.OldElement.UpdateStatus -= OnUpdateStatus;
args.OldElement.PlayRequested -= OnPlayRequested;
args.OldElement.PauseRequested -= OnPauseRequested;
args.OldElement.StopRequested -= OnStopRequested;
args.OldElement.ShowTransportControlsRequested -= OnShowTransportControls;
args.OldElement.HideTransportControlsRequested -= OnHideTransportControls;
}
}
If you put the button in the View, it will be overlapped. So one workaround I thought is add the button to the window and remove it when the controller dismiss:
UIButton btn { get; set; }
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
btn.RemoveFromSuperview();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
btn.RemoveFromSuperview();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
btn = new CustomButton()
{
Frame = new CoreGraphics.CGRect(40, 20, 320, 40),
someValue = 4711
};
btn.BackgroundColor = UIColor.Red;
btn.SetTitleColor(UIColor.Blue,UIControlState.Normal);
btn.SetTitle("Click me", UIControlState.Normal);
btn.TouchUpInside += (sender, e) => {
//btn.RemoveFromSuperview();
};
UIApplication.SharedApplication.KeyWindow.Add(btn);
}
I did not test the remove part as I can't play a video on my side.

OnScrollChanged doesn't get the RecyclerView parameters

I'm trying to implement this code here in my project:
The differences are that instead of a ScrollView I'm using a RecyclerView and that instead of having my RecyclerView directly in the Activity I have inside a fragment that sets the listener I need.
The problem is that while the OnScrollChanged(int l, int t, int oldl, int oldt) gets actually called everytime I scroll, the parameters it obtains are always 0 and I don't understand why, so ScrollChangedTarget doesn't work as intended.
This is the custom RecyclerView:
public class NotifyingScrollRecyclerView : RecyclerView
{
private Activity activity;
private View headerView;
private View footerView;
public delegate void ScrollChangedHandler(int l, int t, int oldl, int oldt, EventArgs e);
public event ScrollChangedHandler scrollChanged;
public EventArgs e = null;
//Enabling all required constructors
public NotifyingScrollRecyclerView(Context context) : base(context)
{
}
public NotifyingScrollRecyclerView(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public NotifyingScrollRecyclerView(IntPtr ptr, JniHandleOwnership ownership) : base(ptr, ownership)
{
}
public NotifyingScrollRecyclerView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
//This method attribute allows us to register the inbuilt OnScrollChanged event that fires when scrolling a ScrollView
[Android.Runtime.Register("onScrollChanged", "(IIII)V", "GetOnScrollChanged_IIIIHandler")]
protected override void OnScrollChanged(int l, int t, int oldl, int oldt)
{
base.OnScrollChanged(l, t, oldl, oldt);
scrollChanged(l, t, oldl, oldt, e);
}
}
//Set an event listener
public class ScrollViewChangedListener
{
private Activity activity;
private NotifyingScrollRecyclerView scrollView;
private Android.Support.V7.App.ActionBar actionBar;
private Drawable actionBarDrawable;
be changed
public ScrollViewChangedListener(Activity a, NotifyingScrollRecyclerView n)
{
n.scrollChanged += ScrollChangedTarget;
this.activity = a;
this.scrollView = n;
this.actionBar = ((UserPageActivity)a).SupportActionBar;
this.actionBarDrawable = a.GetDrawable(Resource.Drawable.actionbar_background);
this.actionBarDrawable.SetAlpha(0);
}
//Handle the changing of the scroll
public void ScrollChangedTarget(int l, int t, int oldl, int oldt, EventArgs e)
{
//You set the View you want to be your header as a header height, and then get it's height
int headerHeight = activity.FindViewById<ImageView>(Resource.Id.profilebanner).Height -
this.actionBar.Height;
float ratio = (float)Math.Min(Math.Max(t, 0), headerHeight) / headerHeight;
int newAlpha = (int)(ratio * 255);
this.actionBarDrawable.SetAlpha(newAlpha);
this.actionBar.SetBackgroundDrawable(this.actionBarDrawable);
}
}
This is the Activity which calls the fragment (it doesn't do anything in particular aside calling the fragment in this case):
[Activity(Label = "UserPageActivity")]
public class UserPageActivity : BaseActivity
{
protected override int LayoutResource => Resource.Layout.activity_user_page;
UserViewModel viewModel;
TextView username;
TextView usernameToolbar;
Button followButton;
ViewPager pager;
UserTabsAdapter adapter;
bool IsLoggedUser;
protected override void OnCreate(Bundle savedInstanceState)
{
IsLoggedUser = Intent.GetStringExtra("userId").Equals(LoginController.GetInstance().CurrentUser.Email);
base.OnCreate(savedInstanceState);
viewModel = new UserViewModel();
viewModel.UserLoaded += new UserViewModel.UserLoadedHandler(OnUserLoaded);
viewModel.LoadUserCommand.Execute(Intent.GetStringExtra("userId"));
username = FindViewById<TextView>(Resource.Id.profilename);
usernameToolbar = FindViewById<TextView>(Resource.Id.usernamePage);
followButton = FindViewById<Button>(Resource.Id.followButton);
username.Text = Intent.GetStringExtra("username");
usernameToolbar.Text = Intent.GetStringExtra("username");
adapter = new UserTabsAdapter(this, SupportFragmentManager);
pager = FindViewById<ViewPager>(Resource.Id.user_viewpager);
var tabs = FindViewById<TabLayout>(Resource.Id.tabs);
pager.Adapter = adapter;
tabs.SetupWithViewPager(pager);
pager.OffscreenPageLimit = 5;
pager.PageSelected += (sender, args) =>
{
var fragment = adapter.InstantiateItem(pager, args.Position) as IFragmentVisible;
fragment?.BecameVisible();
};
}
private void OnUserLoaded(bool loaded)
{
}
protected override void OnStart()
{
base.OnStart();
if (IsLoggedUser)
{
followButton.Visibility = ViewStates.Gone;
}
else
{
bool following;
if (LoginController.GetInstance().CurrentUser.FollowsUsers.ContainsKey(Intent.GetStringExtra("userId")))
{
followButton.Text = "Unfollow";
following = true;
}
else
{
followButton.Text = "Follow";
following = false;
}
followButton.Click += (object sender, EventArgs e) =>
{
followButton.Enabled = false;
if (following)
{
UserService service = ServiceLocator.Instance.Get<UserService>();
service.SetUser(LoginController.GetInstance().CurrentUser);
service.RemoveFollowsUserCommand.Execute(viewModel.LoadedUser.Email);
service.SetUser(viewModel.LoadedUser);
service.RemoveFollowedByUserCommand.Execute(LoginController.GetInstance().CurrentUser.Email);
followButton.Text = "Follow";
following = false;
}
else
{
UserService service = ServiceLocator.Instance.Get<UserService>();
service.SetUser(LoginController.GetInstance().CurrentUser);
service.AddFollowsUserCommand.Execute(viewModel.LoadedUser);
service.SetUser(viewModel.LoadedUser);
service.AddFollowedByUserCommand.Execute(LoginController.GetInstance().CurrentUser);
followButton.Text = "Unfollow";
following = true;
}
followButton.Enabled = true;
};
}
}
}
class UserTabsAdapter : FragmentStatePagerAdapter
{
string[] titles;
public override int Count => titles.Length;
public UserTabsAdapter(Context context, Android.Support.V4.App.FragmentManager fm) : base(fm)
{
titles = context.Resources.GetTextArray(Resource.Array.user_sections);
}
public override Java.Lang.ICharSequence GetPageTitleFormatted(int position) =>
new Java.Lang.String(titles[position]);
public override Android.Support.V4.App.Fragment GetItem(int position)
{
switch (position)
{
case 0: return UserContestsFragment.NewInstance();
case 1: return UserPartecipationsFragment.NewInstance();
case 2: return GlobalContestFragment.NewInstance();
case 3: return MessagesFragment.NewInstance();
}
return null;
}
public override int GetItemPosition(Java.Lang.Object frag) => PositionNone;
}
This is the fragment which setups the listener for the recyclerview:
public class UserContestsFragment : AbstractRefresherFadingToolbarFragment<Contest>
{
public static UserContestsFragment NewInstance() =>
new UserContestsFragment { Arguments = new Bundle() };
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ContestViewModel viewModel = new ContestViewModel();
base.ViewModel = viewModel;
base.LoadItemsCommand = viewModel.LoadAllByCreatorUserCommand;
base.param = Activity.Intent.GetStringExtra("userId");
base.adapter = new ContestsAdapter(Activity, viewModel);
var view = base.OnCreateView(inflater, container, savedInstanceState);
ScrollViewChangedListener listener = new ScrollViewChangedListener(Activity, recyclerView);
return view;
}
And this is the abstract fragment needed by that fragment which is in charge of setting up the layout:
public abstract class AbstractRefresherFadingToolbarFragment<T> : Android.Support.V4.App.Fragment, IFragmentVisible
{
public ICollectionViewModel<T> ViewModel;
public ICommand LoadItemsCommand;
public object param; //parametro per il LoadItemsCommand
public ItemsAdapter<T> adapter;
public SwipeRefreshLayout refresher;
public ProgressBar progress;
public NotifyingScrollRecyclerView recyclerView;
//LruCache cache = new LruCache((int)(Runtime.GetRuntime().MaxMemory() / 4));
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragment_fading_toolbar, container, false);
recyclerView = view.FindViewById<NotifyingScrollRecyclerView>(Resource.Id.recyclerView);
//ScrollViewChangedListener listener = new ScrollViewChangedListener((UserPageActivity)Activity, recyclerView);
//adapter.cache = cache;
recyclerView.HasFixedSize = true;
recyclerView.SetAdapter(adapter);
recyclerView.SetItemViewCacheSize(4);
//recyclerView.ChildViewAttachedToWindow += new EventHandler<RecyclerView.ChildViewAttachedToWindowEventArgs>(OnChildViewAttached);
//recyclerView.ChildViewDetachedFromWindow += new EventHandler<RecyclerView.ChildViewDetachedFromWindowEventArgs>(OnChildViewDetached);
refresher = view.FindViewById<SwipeRefreshLayout>(Resource.Id.refresher);
refresher.SetColorSchemeColors(Resource.Color.accent);
progress = view.FindViewById<ProgressBar>(Resource.Id.progressbar_loading);
progress.Visibility = ViewStates.Gone;
return view;
}
}
I didn't read your entire code but I looked at the site you linked in your question. The functionality of the hidden action bar you want to use is handled by CollapsingToolbarLayout in the support library. To know how to use it, go to Cheesesquare. It is a complete example of the support library widgets and can be built and run without any changes.
Edit:
RecyclerView has a method named AddOnScrollListener. Use it instead of the OnScrollChanged. If you use an inherited class from RecyclerView, you can call it in all of the constructors of that class.

OnClickListeners for Recyclerviews -Xamarin.Droid

I have tried to look out for an answer to the behavior of my views but I seem not find any question or solution related to it. My recycler views seemed to be set up well. I just realized that my app is not responding in the right way to the OnClickListeners.
I have set up toasts in my adapter for the recycler view click events. When i have 10 views. When i click on a view, it gives a text of another view. It seems it randomly gives me the text of another view amongst the 9 remaining views. What could be the cause of this?
Activity
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
FrameLayout content = (FrameLayout)FindViewById(Resource.Id.content_frame);
LayoutInflater.Inflate(Resource.Layout.Main, content);
setUpRecyclerView();
}
public void setUpRecyclerView(){
rv = FindViewById<RecyclerView>(Resource.Id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.Orientation = LinearLayoutManager.Vertical;
layoutManager.ReverseLayout = true;
layoutManager.StackFromEnd = true;
rv.HasFixedSize = true;
rv.SetLayoutManager(layoutManager);
}
Adapter
public class FeedViewHolder : RecyclerView.ViewHolder, View.IOnClickListener, View.IOnLongClickListener
{
public FeedViewHolder(View itemView):base(itemView)
{
//binding of variables here
itemView.SetOnClickListener(this);
itemView.SetOnLongClickListener(this);
}
public void OnClick(View v)
{
itemClickListener.OnClick(v, AdapterPosition, false);
}
public bool OnLongClick(View v)
{
itemClickListener.OnClick(v, AdapterPosition, true);
return true;
}
public class FeedAdapter : RecyclerView.Adapter, ItemClickListener
{
public FeedAdapter(RssObject rssObject, Context mContext)
{
this.mContext = mContext;
this.inflater = LayoutInflater.From(mContext);
activity = (MainActivity)mContext;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
hold = holder as FeedViewHolder;
//binding
hold.itemClickListener = this;
}
public void OnClick(View view, int position, bool isLongClick)
{
Toast.MakeText(activity, "Main text : " + hold.txtContent.Text, ToastLength.Long).Show();
}
public override int ItemCount
{
get { return rssObject.items.Count; }
}
}
}
}

Windows store app navigation

I'm making a Pacman windows store app game. I use win2d library to make animations. I have a problem in navigation between pages.
Here's my main page, it creates new Game.
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
//Game gm = new Game();
}
private void playButton_Click(object sender, RoutedEventArgs e)
{
Game gm = new Game();
}
private void exitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Exit();
}
private void resultsButton_Click(object sender, RoutedEventArgs e)
{
}
}
but in Game class when end finishes I have to somehow comeback to my main page. I have tried many ways but they doesn't work for me.
Game class:
public Game()
{
this.InitializeComponent();
Window.Current.Content = this;
}
private void canvas_CreateResources(CanvasAnimatedControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
async Task CreateResourcesAsync(CanvasAnimatedControl sender)
{
ghostImages = new List<CanvasBitmap>();
ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost1.png")));
ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost2.png")));
ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost3.png")));
ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/ghost4.png")));
ghostImages.Add(await CanvasBitmap.LoadAsync(sender.Device, new Uri("ms-appx:///Assets/Pacman_25.png")));
StartNewGame();
}
private void Canvas_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
Map.drawBorders(args);
using (var session = args.DrawingSession)
{
session.DrawImage(hero.getImage1(), hero.getX(), hero.getY());
for (int i = 0; i < ghostList.ToArray().Length; i++)
{
session.DrawImage(ghostList[i].getImage(), ghostList[i].getX(), ghostList[i].getY());
}
int bestScore = 1, score = 3;
session.DrawText("Rekordas: " + bestScore, Constants.WIDTH / 3 * 1.8f, Constants.HEIGHT + Constants.SHOWINFOSIZE / 2, Windows.UI.Colors.White);
session.DrawText("Rezultatas: " + score, Constants.BLOCKSIZE, Constants.HEIGHT + Constants.SHOWINFOSIZE / 2, Windows.UI.Colors.White);
session.DrawText("Gyvybės: ", Constants.BLOCKSIZE, Constants.HEIGHT + Constants.SHOWINFOSIZE / 1, Windows.UI.Colors.White);
for (int i = 0; i < 3; i++)
session.DrawImage(hero.getImage1(), Constants.BLOCKSIZE + 150 + (Constants.BLOCKSIZE + 5) * i, (int)Constants.HEIGHT + Constants.SHOWINFOSIZE / 1 - Constants.BLOCKSIZE + 5);
}
}
public void GameOver()
{
playing = false;
//Frame.Navigate(typeof(MainPage));
//Dispose();
//this.Dispose();
//var page = new MainPage();
//Window.Current.Content = page;
//MainPage mn = new MainPage();
//if (name == null)
//{
// name = "Student";
//}
//Window.Current.Content = new MainPage();
//mn.UpdateLayout();
}
How can I navigate through pages? Thanks.
Here are some methods that you might find helpful (from a class that I use to wrap navigation logic inside)
//Better made the class a singleton but I've skipped that part to for brifety
public class Navigation
{
public bool CanGoBack
{
get
{
var frame = ((Frame)Window.Current.Content);
return frame.CanGoBack;
}
}
public Type CurrentPageType
{
get
{
var frame = ((Frame)Window.Current.Content);
return frame.CurrentSourcePageType;
}
}
public virtual void GoBack()
{
var frame = ((Frame)Window.Current.Content);
if (frame.CanGoBack)
{
frame.GoBack();
}
}
public virtual void NavigateTo(Type sourcePageType)
{
((Frame)Window.Current.Content).Navigate(sourcePageType);
}
public virtual void NavigateTo(Type sourcePageType, object parameter)
{
((Frame)Window.Current.Content).Navigate(sourcePageType, parameter);
}
public virtual void GoForward()
{
var frame = ((Frame)Window.Current.Content);
if (frame.CanGoForward)
{
frame.GoForward();
}
}
}
You use it like this (if we assume the aforementioned methods reside in a class named Navigation that you have instance of):
//To go to Game page
Navigation.NavigateTo(typeof(Game));
//To go to Main page and pass some arguments
Navigation.NavigateTo(typeof(MainPage), winnerId);
//To go back
Navigation.GoBack();
Addition
You could receive your passed parameters in your views like this:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var receivedParameter = e.Parameter as TheTypeOfThePassedParameter;
}
Additional option to pass data is to create one static or singleton application-wise class (visible from everywhere) containing some values that you want available throughout your app
I suggest you to consider Model View View Model pattern to manage your App's navigation logic and contents. (Channel9 introductive video)
To help you with navigation, you could use MVVM Light library that exposes some useful navigation methods:
In ViewModelLocator.cs you could define a string for every page to be navigated:
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
var nav = new NavigationService();
nav.Configure("MainMenu", typeof(MainMenuView));
nav.Configure("About", typeof(AboutView));
nav.Configure("Game", typeof(GameView));
SimpleIoc.Default.Register<INavigationService>(() => nav);
SimpleIoc.Default.Register<MainMenuViewModel>();
SimpleIoc.Default.Register<AboutViewModel>();
SimpleIoc.Default.Register<GameViewModel>();
}
A typical ViewModel could be:
public class GameViewModel : ViewModelBase
{
private INavigationService _navigationService;
public GameViewModel(INavigationService NavigationService)
{
_navigationService = NavigationService;
}
// Then, when you want to expose a navigation command:
private RelayCommand _navigateToMenuCommand;
public RelayCommand NavigateToMenuCommand
{
get
{
return _navigateToMenuCommand
?? (_navigateToMenuCommand = new RelayCommand(
() =>
{
_navigationService.NavigateTo("MainMenu");
}
{
}
}
And .XAML:
<Button Content="Back to Main Menu" Command={Binding GameViewModel} />

Windows Phone back button not being handled

I have registered a handler with the HardwareButtons.BackPressed event, performed some logic and then set the handled property in the args to true if it applies. The handler runs through without any issue, and the Handled property gets set. The phone still navigates back outside of the app. Am I misunderstanding how to use the event?
Page
public sealed partial class FirstRunPage : VisualStateAwarePage
{
public FirstRunPage()
{
this.InitializeComponent();
#if WINDOWS_PHONE_APP
HardwareButtons.BackPressed += (sender, args) =>
{
bool isHandled = false;
Action handledCallback = () => isHandled = true;
var state = new Dictionary<string, object> { { "Callback", handledCallback } };
((INavigationAware)this.DataContext).OnNavigatedTo("Back", NavigationMode.Back, state);
args.Handled = isHandled;
};
#endif
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
}
View model.
public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
{
if (navigationParameter == null || !navigationParameter.ToString().Equals("Back"))
{
return;
}
if (!viewModelState.ContainsKey("Callback"))
{
return;
}
var callback = (Action)viewModelState["Callback"];
// If the user is new, then we set it to false and invoke our callback.
if (this.IsNewUser)
{
this.IsNewUser = false;
callback();
}
else
{
return;
}
}
Update
I have modified my FirstRunPage to subscribe and unsubscribe as recommended by #Martin but it still closes the app.
public sealed partial class FirstRunPage : VisualStateAwarePage
{
public FirstRunPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
#if WINDOWS_PHONE_APP
HardwareButtons.BackPressed += HardwareBack_OnPressed;
#endif
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
#if WINDOWS_PHONE_APP
HardwareButtons.BackPressed -= HardwareBack_OnPressed;
#endif
}
private void HardwareBack_OnPressed(object sender, BackPressedEventArgs e)
{
Action handledCallback = () => e.Handled = true;
var state = new Dictionary<string, object> { { "Callback", handledCallback } };
((INavigationAware)this.DataContext).OnNavigatedTo("Back", NavigationMode.Back, state);
}
}
With the help of #yasen I was able to get this resolved. The issue stems from the fact that the Prism library has your App.xaml.cs inherit from MvvmAppbase, which intercepts the BackPressed event.
To resolve this, I overrode the MvvmAppBase OnHardwareButtonsBackPressed and added a bit of logic to handle it.
My view model and view both implement a new interface called INavigateBackwards and they're used like this:
View Model
public bool CanNavigateBack()
{
// If the new user is true, then we can't navigate backwards.
// There isn't any navigation stack, so the app will die.
bool canNavigate = !this.IsNewUser;
// Disable the new user mode.
this.IsNewUser = false;
// Return so that the view can return to it's sign-in state.
return canNavigate;
}
View
public sealed partial class FirstRunPage : VisualStateAwarePage, INavigateBackwards
{
private INavigateBackwards ViewModel
{
get
{
return (INavigateBackwards)this.DataContext;
}
}
public FirstRunPage()
{
this.InitializeComponent();
}
public bool CanNavigateBack()
{
return ViewModel.CanNavigateBack();
}
}
Then in the MvvmAppBase subclass, I determine if I need to handle the navigation or not.
MvvmAppBase child
protected override void OnHardwareButtonsBackPressed(object sender, BackPressedEventArgs e)
{
var page = (Page)((Frame)Window.Current.Content).Content;
if (page is INavigateBackwards)
{
var navigatingPage = (INavigateBackwards)page;
if (!navigatingPage.CanNavigateBack())
{
e.Handled = true;
return;
}
}
base.OnHardwareButtonsBackPressed(sender, e);
}
This allows my single view to have multiple states and the user to navigate back from one state to the previous without navigating to an entirely new view.
The reason why your application closes is that the same handler is called more than just once. First handler sets the Handled property to true, but any other subsequent call for the same event fire sets it back to false.
To illustrate it, try this:
public sealed partial class FirstRunPage : VisualStateAwarePage
{
public FirstRunPage()
{
// ...
InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
bool isHandled = false;
Action handledCallback = () => isHandled = true;
var state = new Dictionary<string, object> { { "Callback", handledCallback } };
((INavigationAware)this.DataContext).OnNavigatedTo("Back", NavigationMode.Back, state);
args.Handled = isHandled;
};
}
And set breakpoint to last line of the handler code.
To avoid it, assign your handler in the OnNavigatedTo method of your FirstRunPage, and unregister the handler in OnNavigatedFrom.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}

Categories