Windows 8 App Store - Navigation issue - c#

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)

Related

Disable runtime cache in Microsoft.Web.WebView2.Wpf

I am creating a webview2 wpf application, everything works well till now, but when I tried to load HTML file which is having youtube link, It's worked well along with autoplay .
But when I change the HTML file to some another site , the audio of youtube video still playing in background and webview2 load the new content.
I want something to disable the cache to be stored will runtime instead of clearing the cache.
If someone is having any idea related to above issue please help
This is the common issue faced by the webview2 when external site media player is played.
To overcome this issue you have to create a new instance of webview2 before you load second content to webView, this will create new Webview Page but still the UI will not stop the audio, so use Webview.dispose() as well.
WebView.Dispose()
Note : this will give you issue related to Null object reference for the 1st time .
So you have the check the the object before doing dispose.
Code should be like this :
MainPage :
WebViewPage webpage;
private void updateNewContent ()
{
...
WebView2 webView2 = Mywebvew2; // MYwebview2 is UI object from webviewPage,
//you have to pass this object from webview Page
if (webView2 != null)
{
webView2.Dispose();
}
webpage = null;
webpage = new WebViewPage();
GridPrincipal.Children.Add(webpage);
...
}

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.

Holding on to data passed from page to page - WP7 and C#

I'm creating a WP7 application using C#, and I require to pass data from one page to the other.
I found solutions on SO, but I'm still running into problems.
On 'Page 1', I wish to display a list, that can be populated by the user, using input from 'Page 2'.
I used the following statement in 'Page 2' while navigating back to 'Page 1': NavigationService.Navigate(new Uri("/MainPage.xaml?text="+WhoBox.Text, UriKind.Relative));
WhoBox is a Text Box.
On 'Page 1', I have the following:
protected override void OnNavigateTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("text"))
ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"];
}
Now, this works, but in a limited fashion. If I try adding something from 'Page 2' for a second time, it replaces what is present in ListBlock (which is a Text Block) with the newly added text instead of appending it.
Shouldn't ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"]; cause the new text to be appended, rather than to entirely replace the older text?
EDIT: I may have found the solution. For whatever reason, no changes in the XAML or .cs file are reflected when I run the program using F5. Am I doing something wrong? For example, even if I delete a button, it still appears when I Debug (F5) the program. Is there some setting I need to change? Or am I supposed to use some other command? I'm relatively new to Visual Studio, so please excuse me.
The problem is that the moment you again leave your page 1 it is basically disposed of. Meaning any text that was set in the Listbox is also removed. You will , in other words, need to save the state of that page before leaving it.
There several possibilites here:
Use AppSettings (see Windows phone 7 config / appSettings? )
Write the state to a local database
Do a quick'n dirty fix by saving the Text in the App.xaml.cs which all pages can work with: First you need to create the application-wide variable (and initialize if needed) inside the app.xaml.cs file. For example:
public partial class App : Application
{ public string myText;
From now on you can reach any App-variable through the Application.Current object. So if you need to access bigVar from some page in you application (e.g. MainPage) you simply type:
string Text = (Application.Current as App).myText;
Consider using sessions and datatable: Storing and retrieving datatable from session

How to Target the current frame in Silverlight using 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))

Categories