How to Target the current frame in Silverlight using C#? - c#

If I have a button in a navigation page, how do I make it target the frame that it is in?
For example, I have a frame called navFrame in MainPage.xaml and I have a navigation page in the Views folder called Home.xaml with a button on it. Basically I'm trying to make clicking the button Home.xaml target the navFrame in MainPage.xaml.
Is that possible? for the button code I tried using MainPage.navFrame.Navigate(...) but it gave me the error "an object is required to reference a non static property..." but I don't know what kind of object it wants or how I would use it.
I'm sorry if this is a stupid question. I'm new to all of this and I spent all last night trying to figure out. Any help would be really appreciated!

From the error message it seems that you don't have a reference to the instance of MainPage in the Home class.
You could get a reference to it through searching up the Visual Tree or calling Application.Current.RootVisual or using some kind of locator framework.
But you don't really need to get a reference to MainPage. From inside the Home : Page class you could instead try
this.NavigationService.Navigate(new Uri("/About", UriKind.Relative))

Related

Previous page instance not maintained after navigation in prism

I am using the latest version of Prism.MVVM in Xamarin.Forms. In this, if I try to navigate to second page from the first page, the first page is initialized once again. i.e., the constructor of the first page is called once again.
For example, I am having Page1.xaml and Page2.xaml pages with their respective view models(those will be created and registered automatically while creating in prism).
I navigating to Page2 from Page1 like below,
NavigationAsync("Navigation/Page1/Page2")
While navigating, Page1.xaml's constructor is called so that the page is created newly which lead I could not able to maintain the Page1.xaml instance. Also, please note that Page1.xaml is a Master-Details page.
Is this a behavior in Prism? If so how can I overcome this?
Thanks in advance.
Navigating away from a XAML page destroys it in UWP. You can preserve a page’s state data (and avoid re-construction) by adding a single line in a XAML page’s tag:
NavigationCacheMode="Required"
Does it work the same in Xamarin?

How does FindViewById() work?

I'm new in mobile app development. I'm using Xamarin to develop Android applications. In the hello world app in the OnCreate method I see the following code:
Button button = FindViewById<Button>(Resource.Id.MyButton);
So I'm trying to create my own button the same way. I create the button in the designer and inside OnCreate method put the line:
Button myOwnBtn = FindViewById<Button>(Resource.Id.MyOwnBtn);
That gives me an error that there is no MyOwnBtn. Then I'm looking the code of Id class and see there a line like:
public const int MyButton=2123344112;
If I put there the line:
public const int MyOwnBtn=2123344113;
Everything works fine. But as I understand it should be generated automatically or it will be a little bit difficult to put there a unique number for each control.
Can anybody tell me what I am doing wrong? And how does FindViewById() work?
You have to give the id MyOwnBtn to the Button that you created in the designer.
findViewById is a method of the View class and it looks for a child view having the id that you provided in the argument.
From official documentation:
Look for a child view with the given id. If this view has the given id, return this view.
MyButton id is not a const value, It will change every launch.
The Activity or ViewGroup's findViewById() method returns a view that already has an id. The findViewById() method should be used in conjunction with XML layouts to provide a reference to the View that was defined in the XML file.
Edit: Not entirely sure if my answer is relevant to Xamarin. I apologize if I have mislead people, I am referring to Java Android application development.
When you declare a button in your .xml file, you should set an id for it (Usually it is done using string.xml file). After that, R.java will be updated automatically and set a number to your declared id and you can access your button by that id like what you have done.
It will try to find it from the XML file that you inflate. So make sure you inflate the correct xml file. This code inflates the xml:
SetContentView (Resource.Layout.MainLayout);
Even if you got the correct id created in a xml file, if you don't inflate it first, the system won't be able to find that view since it is not inflated.

Navigation between pages in WP8.1

I'm trying to migrate my app from WP8 to WP8.1. And I don't get how to navigate to already opened page with another parameters.
For example, I'm showing user info on UserPage giving it user's id as parameter. And when page is already is the content of the Frame I want to open UserPage again but for other user giving it another id.
My problem is that, using NavigationCacheMode set to Required for UserPage means that there will be no navigation with other parameters. But when NavigationCacheMode is set to Disabled navigation with another parameter is success but when I press back button old instance of UserPage is using data from new one.
In WP8 passing new parameters was enough to create new instance of a page with it's own cache. How to do similar in WP8.1 using WRT APIs?
Thanks to Romansz for the tip about using UserControl. Using UserControl binding to a ContentControl and handling BackKeyPress solves my problem with navigation.

Windows 8 App Store - Navigation issue

I'm having an issue with a Windows 8 App Store application that I'm trying to write.
I'm trying to navigate to a new page. I'm using this code in my MainViewModel:
var page = (Window.Current.Content as Frame);
page.Navigate(typeof(Home));
Then in my HomeViewModel I'm trying to access the Home view so that I could get some stuff to work, I'm using this code:
var page = (LayoutAwarePage)(Window.Current.Content);
When I run my application it tells me:
Object reference not set to an instance of an object.
and when I place my mouse over
Window.Current
, I see it is set to
NULL
... So how is this possible? Am I missing something?
If I understand you correctly, I think what you might need to do is to move the code that references the 'Home' page in Home.xaml.cs instead of in HomeViewModel.cs. Call a method in HomeViewModel.cs from Home.xaml.cs - hope this helps. (I am assuming your HomeViewModel.cs is a class, which you'll probably need to instantiate in Home.xaml.cs)

Referencing an ASP control within a LoginView

Another total newb question, I'm afraid: I have a LoginView with some HyperLinks inside it, but when I try to reference the HyperLink in the code behind it tells me that it doesn't exist in the "current context".
eg. hypLink1.NavigateUrl = "some/link/on/my/site.aspx"
I've figured out that it's only because it's in the LoginView that it can't find it... so what can I do to tell it to look inside the LoginView?
I thought it might be something intuitive like:
LoginView1.hypLink1.NavigateUrl = "some/link/on/my/site.aspx"
But to no avail.
Thanks for any help with this (most likely) really obvious problem!
I'm guessing that you're trying to reference the hyperlink from outside the loginview control?
At that point, you could try a FindControls operation on the LoginView:
HyperLink hypLink1 = (HyperLink)LoginView1.FindControls("hypLink1");
UPDATE
Ok, so I was confused as to what you were asking. The LoginView control only allows FindControls, and so you have to use the code snippet up above in order to reference controls internal to it.
Since the LoginView control uses templates, different controls will exist under different circumstances. As such, the code cannot ensure any given control inside the template will be alive at compile time.
So you'll have to FindControls every time you want to get a child control :'(

Categories