Free Windows phone page memory - c#

I have a simple WP8 app with 2 pages. In MainPage the user can navigate to Page2 to see a couple of images from the net. To do this I use a WebClient
WebClient myWebClient;
myWebClient = new WebClient();
myWebClient.DownloadStringAsync(mywebsite);
myWebClient.DonwloadStringCompleted += new DownloadStringCompletedEventHandler(myWebClient_Completed);
Afther that, I display the images in a StackPanel wrapped inside a ScrollViewer that looks like this
<ScrollViewer>
<StackPanel x:Name= myPanel/>
</ScrollViewer>
When i try to navigate back and forth between the pages, the app crashes after several times. I believe this is a memory leak.
My question is: What are the steps that need to be done when navigating away from Page2?
I tried to unhook the event handler:
myWebClient.DonwloadStringCompleted -=myWebClient_Completed;
and tried to free the Images from the StackPanel:
foreach(var theImage in myPanel.Children){
theImage.Source=null;
}
But it's no use. What am I missing? or maybe the syntax isn't right?
Thanks for help in advance.

Related

Windows IoT Web Browser

I want to setup my raspberry pi 3 that is running Windows 10 IoT to display a webpage, specifically this page. My issue is, that looking at IoTBrowser sample code, and I can't figure out how to remove the buttons and address bar and get the webpage to autoload. So I'm asking if anyone has code I can use for it?
The instructions I was trying to follow for removing all the display elements except for the webpage its self can be found here, but it really confuses me.
My issue is, that looking at IoTBrowser sample code, and I can't
figure out how to remove the buttons and address bar and get the
webpage to autoload.
You can remove all buttons and address bar except webView in xaml and utilize OnNavigatedTo method to achieve autoload purpose. Then what you need are a few lines code like this:
In MainPage.xaml :
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="webView"/>
</Grid>
In MainPage.xaml.cs :
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var address = "https://www.windowsondevices.com";
webView.Navigate(new Uri(address));
}

uwp youtube player stream mvvm

Solution at the end.
I have a hard time finding informations about a youtubeplayer in uwp following the MVVM pattern.
What are the differences between <MediaElement> and <WebView> in this context and what would be better/easier to use?
How do I handle the events of the Player in my VM?
VS2017 tells me that i can't use the MediaPlayerElement
Error Message
I tried other versions/builds of VS2017 but get the same error.
Are there other ways to do this?
TY
Edit:
<WebView> seems to be pretty easy. Are there any downsides using it?
<WebView x:Name="webView" Width="300"
Height="225" Source="https://www.youtube.com/watch?v=2Rg1XmzzrTM"
RelativePanel.AlignHorizontalCenterWithPanel="True"
RelativePanel.Below="linkYT"/>
Edit2:
Thanks to Aditya Sharma I got on the right Track and found this Question
So I ended up with the following:
View:
<MediaElement x:Name="ytPlayer"
AreTransportControlsEnabled="True"
Source="{Binding Test1, Mode=OneWay}"
Width="600"
AutoPlay="True"
Height="400"/>
VM:
public void doSomething()
{
...
test1 = new Uri(convertLink(currentMovie.TrailerSource));
...
}
private string convertLink(string link)
{
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link, false);
VideoInfo video = videoInfos
.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 720
);
return video.DownloadUrl;
}
Where Test1 is of Type Uri and currentMovie.TrailerSource is a string
For this to work I had to get the NuGet YouTubeExtractor.
Hope this Helps somebody get it done MVVM.
So here is the thing, We have 1. WebView and 2. MediaElement
Let's take up a deep dive into these:
WebView: is basically a phone in app browser. you can give it a link and it'll navigate to the webPage. You can use it for your youtube screening but there will be downsides:
You won't be able to use native controls like pause/play/seek locally. All the controls will be handled by the website rendered in a WebView.
You have no control on the UI as well. You'll see the basic embedded youtube UI in your app WebView that might just break your UI and might also just provide a pixelated view when scaled.
MediaElement on the other hand is quite convenient, it has a source property that takes a Uri datatype as well so the great thing is that you can pass in your Uri as the embedded link for the youtube vid, the MediaElement will handle everything for you plus, it'll give you some additional advantages like control over your UI, Play/Pause or even custom styling and autoplay/playRate options.
So In my opinion, I would say use a MediaElement over the webView as it'll provide you with more control over whats happening and also provide a native experience to the user.
You can find some more information on it here
in this question and in it's comments section.
Also, since you'll be using mvvm on windows 10, I would recommend you x:bind the Source property of the MediaElement to a Uri DataType property in a oneWay Mode in your ViewModel

How to store state in Windows 8.1 using Blank Page Template?

I am migrating my Windows Phone 8 App to Windows 8.1 App and I have created a Page with blank page template. In that there are some Items like TextBlock,ComboBox and TextBox. Now, there is an item LocationTextBlock with border around it. When I click on it, it navigates to a new Page LocationPage, where I need to select location and save that object and navigate using Frame.GoBack() method. Now the issue, when I navigate back, I get the object of LocationData but the LocationTextBlock which was created previously is showing null, so I can't populate Location in that TextBlock.
Questions
Is this issue occurring due to Blank Page Template ?
Can it be solved using NavigationHelper Class?
Will using Basic Page Template resolve this issue?
Please suggest with some code or description, whether it can be done using Blank Page template as I have added many lines of code inside it.
It can be done with Blank Page template (although using NavigationHelper would be the prefered way).
What you have to do is set NavigationCacheMode to Enabled in your Page's Constructor:
this.NavigationCacheMode = NavigationCacheMode.Enabled;
This way the Page with all the properties in it is cached. So when you navigate back you get back the state in which the Page was when you navigated from it. However, if you navigate to this page not through backnavigation, you'll still get back the saved state, which you don't want. The solution is to clean up all the resources (initializing the needed variables, setting UI elements to default value etc.) in the OnNavigatedTo(NavigationEventArgs e) method if (e.NavigationMode != NavigationMode.Back). Don't know if it's the best approach, but it'll work.
EDIT:
You could even use a Flyout or a ContentDialog instead of your LocationPage, so you don't need to navigate from the page, thus not needing to cache/save the page.

How to download source code of website after having emulated a button click?

With WebClient I can download the source code.
WebClient.DownloadString(url);
With WebBrowser I can emulate mouse click, getting the HTML element by Id and invoking it.
WebBrowser.Document.GetElementById("commit").InvokeMember("click");
My question is: How can I mix these to:
Press the button (one time or multiple times, the id remains unchanged).
Download updated source code (after button click).
P.S. As you can guess the button is a "View more" button that loads new elements, and the url stays the same all the time, there is no page 2. That's why I have a problem.
P.S. This looks like my problem, but in my case it's the same page, not a new window
Let me tell you that it makes no sense to use the WebBrowser and WebClient in conjunction like that. The WebBrowser is not an abstraction of the WebClient or anything like that, they are completely different and they are completely separate.
Assuming you want to persue this problem using the WebBrowser, you could download the page source using the WebBrowser component like so:
webBrowser1.Document.GetElementById("commit").InvokeMember("click");
webBrowser1.DocumentCompleted += (o, args) =>
{
string pageSource = webBrowser1.DocumentText;
};

How to create master page for Windows8 app using Xaml?

I need to create a Windows8 application but there are multiple elements like progress bar, same app bar and other components that are been used over and over again. In previous application i have been applying these elements in every page. Is there some approach by which I can make a master page and inhert an use it in every page. As we can do in ASP.Net Master page concept?
You have to deal with frames :
<Page x:Name="MainPage">
<Grid x:Name="LayoutRoot">
<Frame x:Name="BasicLayout"/>
<Frame x:Name="SpecificLayout"/>
</Grid>
</Page>
Use BasicLayout to show components that are been used over and over again.
Use SpecificLayout to show your page-specific content (So you don't directly navigate from the "master" Frame, but from the SpecificLayout one).
Here is nice tutorial how to create master page for Windows 8 application
Windows 8 master page tutorial

Categories

Resources