How to get the Device Resolution in Wp7 - windows-phone-7

I want to set some icons in my app according to device screen resolution like wxga or wvga. I saw in many links like App.Current.Host.Content.ScaleFactor or Application.Current.RootVisual.RenderSize. But I can only access App.Current.Host.Content.ActualWidth or Height. These always say as 480x800 even though I am running the app in wxga Device. How do i know the resolution correctly ?

Windows Phone 7 only supports one resolution(800*480). Are you asking about Windows Phone 8? Please a look at Multi-resolution apps for Windows Phone 8 . Here is the ResolutionHelper class you can use.
public enum Resolutions { WVGA, WXGA, HD720p };
public static class ResolutionHelper
{
private static bool IsWvga
{
get
{
return App.Current.Host.Content.ScaleFactor == 100;
}
}
private static bool IsWxga
{
get
{
return App.Current.Host.Content.ScaleFactor == 160;
}
}
private static bool Is720p
{
get
{
return App.Current.Host.Content.ScaleFactor == 150;
}
}
public static Resolutions CurrentResolution
{
get
{
if (IsWvga) return Resolutions.WVGA;
else if (IsWxga) return Resolutions.WXGA;
else if (Is720p) return Resolutions.HD720p;
else throw new InvalidOperationException("Unknown resolution");
}
}
}

You Target your app for Windows Phone 7.1 , so you should update your app to target windows phone 8.0 OS by clicking right on your project and Upgrade to Windows Phone 8.0

Related

Custom renderer for LibVLCSharp VideoView in Mac and UWP (Xamarin.Forms)

I'm trying to use VideoView from LibVLCSharp for Mac to create a custom renderer in Xamarin.Forms to play a video in Xamarin.Forms mac application. So far I only get audio but no video.
this is my VideoPlayerRenderer for mac implementation
[assembly: ExportRenderer(typeof(Player.VideoPlayer), typeof(Player.Mac.VideoPlayerRenderer))]
namespace Player.Mac {
public class VideoPlayerRenderer : ViewRenderer<VideoPlayer, LibVLCSharp.Platforms.Mac.VideoView> {
LibVLCSharp.Platforms.Mac.VideoView video_view;
public VideoPlayerRenderer() {
}
protected override void OnElementChanged (ElementChangedEventArgs<VideoPlayer> e) {
base.OnElementChanged (e);
if(e.OldElement != null) {
}
if(e.NewElement != null) {
if(Control == null) {
video_view = new LibVLCSharp.Platforms.Mac.VideoView();
video_view.MediaPlayer = e.NewElement.get_media_player();
SetNativeControl(video_view);
}
}
}
}
}
and the VideoPlayer Xamarin.Forms View
public class VideoPlayer : View {
LibVLC lib_vlc;
MediaPlayer media_player;
public VideoPlayer() {
}
public void init(LibVLC lib_vlc, MediaPlayer media_player) {
this.lib_vlc = lib_vlc;
this.media_player = media_player;
}
public void play() {
this.media_player.Play();
}
public MediaPlayer get_media_player() {
return this.media_player;
}
}
I've tried the same method on UWP and there i get no audio nor video. So i'm wondering if this is going in the wrong direction, and if so, how are you supposed to go about using LibVLCSharp for mac/uwp?
You don't have to create your own renderer since there is already one.
From the LibVLCSharp.Forms documentation :
This package also contains the views for the following platforms:
Android
iOS
Mac
The UWP support for Xamarin.Forms currently has blockers that we expect to get solved by the LVS 4/ libvlc 4 release. See this issue for a detailed explanation.

In Xamarin app, tabs from TabbedView not Populated at the Same Time in Android Tablet emulator as in Android Phone emulator

I am trying to set icons on tabs in my Xamarin app's TabbedView based on the state of the page displayed on the tab. I am able to access a tab individually and setting its icon during rendering on an Android phone emulator, but it does not work on Android tablet emulator.
I am using the DispatchDraw method in a TabbedRenderer based on this post, but I don't have a good understanding though of how that method is called and am having difficulty finding guidance on it.
My code does get called in both devices, but I find that (this.Context as Activity).ActionBar.TabCount is 0 in the tablet every time I check it, while it it is 0 initially but then DispatchDraw is called a second time and its value at that point is 2 (i.e. the tabs have loaded) on the phone, but not on the tablet. That's what allows me to set the icon on the phone, but not the tablet.
Any idea what would differ based only on the change of emulators, to make them render differently in this way?
Here is my TabbedRenderer code:
[assembly: ExportRenderer(typeof(FormTabbedPage), typeof(ExtendedTabbedPageRenderer))]
namespace MyNamespace
{
public class ExtendedTabbedPageRenderer: TabbedRenderer
{
public ExtendedTabbedPageRenderer(Context context) : base(context) { }
protected override void DispatchDraw(global::Android.Graphics.Canvas canvas)
{
base.DispatchDraw(canvas);
SetTabIcons();
}
private void SetTabIcons()
{
var element = this.Element;
if (null == element)
{
return;
}
Activity activity = this.Context as Activity;
if ((null != activity && null != activity.ActionBar && activity.ActionBar.TabCount > 0))
{
bool hasErrors = false;
for (int i = 0; i < element.Children.Count; i += 1)
{
hasErrors = ((i % 2) > 0); // This is a placeholder for app specific logic based on page content
int resourceID = Resources.GetIdentifier("error", "drawable", Context.PackageName);
if (hasErrors)
{
Drawable icon = Context.GetDrawable(resourceID);
Android.App.ActionBar.Tab tab = activity.ActionBar.GetTabAt(i);
tab.SetIcon(icon);
}
}
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// do nothing - I have this method for debugging purposes
}
}
I find that on the tablet, if I initially render the form, I don't get icons, but if I then click on one of the tabs, and if I run the tab logic in OnElementPropertyChanged method instead of DispatchDraw at that point, by then the tabs on the ActionBar ARE populated and I DO get the icons showing.
(Sidenote: one other odd thing is that there is a warning over the ActionBar class saying it's obsolete - not sure what to make of that, since the Activity class contains it as a property and that isn't obsolete.)
Many thanks!

Xamarin UI Testing

I would like to add UnitTest into my Xamarin app, I get the splash screen displaying on the Andriod emulator and then the landing page loads but the next page it seems to closes the app . I have break point around the Platform.Android and that hits but it never hit the end of that method. I added WaitTimes for 5 minutes and I will get the timeout exception
Any idea why the app closes ?
static class AppManager
{
private const string ApkPath = #"C:\pathtoapkfile.apk";
static IApp app;
public static IApp App
{
get
{
if (app == null)
throw new NullReferenceException("'AppManager.App' not set. Call 'AppManager.StartApp()' before trying to access it.");
return app;
}
}
static Platform? platform;
public static Platform Platform
{
get
{
if (platform == null)
throw new NullReferenceException("'AppManager.Platform' not set.");
return platform.Value;
}
set { platform = value; }
}
public static void StartApp()
{
if (Platform == Platform.Android)
{
app = ConfigureApp
.Android
.ApkFile(ApkPath)
.WaitTimes(new WaitTimes())
.StartApp(AppDataMode.Clear);
}
if (Platform == Platform.iOS)
{
app = ConfigureApp
.iOS
.StartApp(AppDataMode.Clear);
}
}
}

Simple navigation in Windows 8 web view

I'm working on porting a Windows Phone 8 application to tablet, and I've bumped into a problem with the WebView API. In Windows Phone 8 and Windows 8.1, the WebBrowser and WebView controls both have a GoBack() method. However, I need my application to be compatible for Windows 8, whose WebView API does not have such a method. Are there any alternatives/workarounds that anyone's used for Windows 8 apps?
In the end I just ended up writing a wrapper for the WebView to manage the navigation stack. Here's the relevant code, for anyone who's interested. Note that I only needed to handle backwards navigation, so I used a Stack. If forwards navigation is also required, it'd probably make sense to replace the Stack with a List and store the index of the current page instead.
public class WebViewWrapper
{
private Stack<Uri> _navigationStack;
private Uri _currentUri;
public WebView WebView { get; private set; }
public bool CanGoBack
{
get { return _navigationStack.Count > 0; }
}
public WebViewWrapper(WebView _webView)
{
_navigationStack = new Stack<Uri>();
WebView = _webView;
WebView.LoadCompleted += (object s, NavigationEventArgs e) => {
if (_currentUri != null)
{
_navigationStack.Push(_currentUri);
}
_currentUri = e.Uri;
};
}
public void GoBack()
{
if (CanGoBack)
{
_currentUri = null;
WebView.Navigate(_navigationStack.Pop());
}
}
}
An example of usage would be as follows:
// Code behind for a view called WebBrowserPage
public sealed partial class WebBrowserPage : Page
{
private WebViewWrapper _webViewWrapper;
public WebBrowserPage()
{
// webView is a WebView in the xaml with x:Name="webView"
_webViewWrapper = new WebViewWrapper(webView);
}
// Other code for navigating to a Uri specified in a ViewModel.
// Event handler for a back button press
private void BackButton_Click(object sender, RoutedEventArgs e)
{
if (_webViewWrapper.CanGoBack)
{
_webViewWrapper.GoBack();
}
else
{
// Code that executes a command in the ViewModel to leave the WebBrowserPage
}
}
}
WinRT XAML Toolkit has a WebBrowser control that does some of that, but I haven't used it in any app, so I can't vouch for its quality.

Emulators Providing Different Results

I am developing an application on windows phone with version 7.1 set as my target build. The problem i am having is that one of the listviews in on of my pages refus to display.
I have debugged to ensure the list gets parsed with contents inside of it . Also the application runs fine when i use a windows 8 emulator. But the same technique used in populating other listviews in other pages of the application work fine on all emulators aprt from this single page that does not display.
I even tried to set the colour of the binding stack panel to see if it will show up and it does but without any content.
I am really confused and my code is very perfect. I wonder if any one has seem this issue before with windows phone emulators?
private void countdownClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
HtmlDocument doc = new HtmlDocument();
if (e.Error != null)
{
//MessageBox.Show(e.Error.InnerException.Message + "\n Ensure You Have A Working Internet Connection");
return;
}
doc.LoadHtml(e.Result);
String noCountdown = "<div><span>Sorry no buses are expected within 30 minutes of this stop. Please try again later or go to www.tfl.gov.uk</span></div>";
if (e.Result.Contains(noCountdown))
{
//No Buses Expected;
return;
}
else
{
HtmlNode stopCountdownNode;
try
{
stopCountdownNode = doc.DocumentNode.SelectSingleNode("//*[contains(#id, 'stopBoard')]").SelectSingleNode("tbody");
}
catch (Exception)
{
MessageBox.Show("Error Responce From Server");
return;
}
if (stopCountdownNode != null)
{
HtmlNodeCollection countdownNodeList = stopCountdownNode.SelectNodes("tr");
CountDownListBox.ItemsSource = GetCountdownList(countdownNodeList);
}
}
}
private ObservableCollection<BusCountdown> GetCountdownList(HtmlNodeCollection countdownNodeList)
{
ObservableCollection<BusCountdown> countdownList = new ObservableCollection<BusCountdown>();
foreach (HtmlNode countDown in countdownNodeList)
{
String busName = HttpUtility.HtmlDecode(countDown.SelectSingleNode("*[contains(#class, 'resRoute')]").InnerHtml);
String busDestination = HttpUtility.HtmlDecode(countDown.SelectSingleNode("*[contains(#class, 'resDir')]").InnerHtml);
String countDownTime = HttpUtility.HtmlDecode(countDown.SelectSingleNode("*[contains(#class, 'resDue')]").InnerHtml);
countdownList.Add(new BusCountdown(busName, busDestination, countDownTime));
}
return countdownList;
}
public string GetRandomSlash()
{
Random r = new Random();
String slash = "";
int rand = r.Next(1, 20);
for (int i = 0; i < rand; i++)
{
slash += "/";
}
return slash;
}
Try setting your class access specifier which you use to bind to public and give it a try. Let me know if it works.
For ex:
public class Bindingclass
{
public string Name{get;set;}
}
Try using Expression Blend and also delete your previous solution file and build a new solution.
Also set the build action property properly for all pages.
Update your SDK to 7.8 version. You will get multiple choices for Emulators - Emulator 7.1 (256 MB), Emulator 7.1 (512 MB), Emulator 7.8 (256 MB), Emulator 7.8 (512 MB). Test it on all these versions and check output on each Emulator type.
I hope at least one of these helps you get things get working. Let us know.

Resources