Xamarin.Forms - App does not open - xamarin

I am learning Xamarin.Forms with Visual Studio 2015 on a Windows 10 machine. When I create a Xamarin.Forms(Android/iOS) cross-platform project and start the AVD, the emulator works, but the application does not open. I don't receive errors in Visual Studio.
It's just a test:
public App()
{
// The root page of your application
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "Welcome to Xamarin Forms!"
}
}
}
};
}

After a lot of effort i update my android sdk tools and all starts work again, thx everyone. Version 25.2.3.

Related

Toast notification not working in Xamarin UWP Windows app

I have UWP Windows application, developed under the Xamarin.forms. I have implemented the Toast notifications but I am facing the issue with this. In some Windows 10 systems, it is working and showing the toast notification properly, but in some of the Windows 10 systems (even having the same Windows 10 OS update) it is not working.
Below first code snippets that I have implemented in the Native UWP.
string msg = "Toast Notification Header";
string subMsg = "Toast Notification Title";
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode(msg));
toastTextElements[1].AppendChild(toastXml.CreateTextNode(subMsg));
//To play the custom sound
var toastNode = toastXml.SelectSingleNode("/toast");
var audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-appx:///Assets/incoming_message.wav");
audio.SetAttribute("loop", "false");
toastNode.AppendChild(audio);
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
Below second code snippets that I have implemented in the Native UWP.
// "With Microsoft.Toolkit.Uwp.Notifications"
// Construct the toast content
ToastContent toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "Toast Notification Header"
},
new AdaptiveText()
{
Text = "Toast Notification Content"
}
}
}
}
};
bool supportsCustomAudio = true;
// If we're running on Desktop before Version 1511, do NOT include custom audio
// since it was not supported until Version 1511, and would result in a silent toast.
if (AnalyticsInfo.VersionInfo.DeviceFamily.Equals("Windows.Desktop")
&& !ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2))
{
supportsCustomAudio = false;
}
if (supportsCustomAudio)
{
toastContent.Audio = new ToastAudio()
{
Src = new Uri("ms-appx:///Assets/incoming_message.wav")
};
}
// And create the toast notification
ToastNotification notification = new ToastNotification(toastContent.GetXml());
// And then send the toast
ToastNotificationManager.CreateToastNotifier().Show(notification);
Above code snips showing the Toast notification in some Windows 10 system and not working in some other Windows 10 system.
Kindly guide me on this. Thanks in advance.
Regards,
Vivek
Please follow these steps to add toast notification in UWP Project.
Step 1:- Create a new UWP project.
Step 2:- Go to the code-behind and add the namespace.
using Windows.UI.Notifications;
using
NotificationsExtensions.Toasts;
Step 3:- I created a Toast Generic Template like the following code:
public static Windows.Data.Xml.Dom.XmlDocument CreateToast()
{
var xDoc = new XDocument(
new XElement("toast",
new XElement("visual",
new XElement("binding", new XAttribute("template", "ToastGeneric"),
new XElement("text", "C# Corner"),
new XElement("text", "Do you got MVP award?")
)
),// actions
new XElement("actions",
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "Yes"), new XAttribute("arguments", "yes")),
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "No"), new XAttribute("arguments", "no"))
)
)
);
var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
return xmlDoc;
}
Step 4:- Create a toast notification object using XML document.
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
Next show the toast using ToastNotificationManager class.
var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
notifi.Show(toast);
Step 5:- C# code-behind:
private void showToastBtn_Click(object sender, RoutedEventArgs e)
{
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
notifi.Show(toast);
}
I hope the above code will be useful for you.
Thank you

LoadApplication(new App()) throws error on IOS! Working fine on Android Xamarin forms

My application is working as desired on Android but when I try to run it on ios (Emulator/device) it throws Null reference exception.
Here is the code in App.xaml.cs
public App()
{
InitializeComponent();
if (true)
{
if (CrossConnectivity.Current.IsConnected)
{
do something
}
MainPage = new MyApp.MenuItems();
}
else
{
MainPage = new MyApp.MainPage();
}
}
Please help as app is fully ready for android and something going wrong with ios.
try with
switch(Device.RuntimePlatform)
{
case Device.iOS:
MainPage = new NavigationPage(new MyApp.MainPage());
break;
case Device.Android:
MainPage = new MyApp.MainPage();
break;
}
may be it's help you
are you sure that you have installed connectivity plugin into ios project too?

How to change font size of navigationPage in Xamarin?

I have a Xamarin.Form project.
I want to change the font size of navigationBar in Win phone 8.1.
Here is my code.
public partial class App: Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new LoginPage))
{
BarBackgroundColor = Color.Red,
//BarFontSize = "Large"???
};
}
}
How can I change fontsize of navigation bar?
Use Custom Render
https://forums.xamarin.com/discussion/22825/how-to-change-title-font-for-navigation-bar
https://github.com/daniel-luberda/XamarinFormsToolbarCustomFont
You can also use this plugin
https://github.com/daniel-luberda/XamarinFormsToolbarCustomFont

Webview BaseURL in Xamarin.Forms on UWP and Windows Phone 8.1

In my shared portable Xamarin project, this works on UWP, Windows Phone 8.1 and Windows 8.1:
HtmlWebViewSource htmlSource = new HtmlWebViewSource();
htmlSource.Html = #"<html><body><img src='ms-appx-web:///Assets/somePicture.png' /></body></html>";
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
WebView webView = new WebView
{
Source = htmlSource,
};
Obviously, this isn't cross-platform (iOS and Android). I want this, but it doesn't work on UWP, Windows Phone 8.1 and Windows 8.1:
HtmlWebViewSource htmlSource = new HtmlWebViewSource();
htmlSource.Html = #"<html><body><img src='somePicture.png' /></body></html>";
htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
WebView webView = new WebView
{
Source = htmlSource,
};
IBaseUrl:
public interface IBaseUrl { string Get(); }
BaseUrl implementation for UWP, Windows Phone 8.1 and Windows 8.1, taken from a Windows Phone 8.0 sample project:
[assembly: Dependency(typeof(MyApp.UWP.BaseUrl))]
namespace MyApp.UWP
{
public class BaseUrl : IBaseUrl
{
public string Get()
{
return ""; // "ms-appx-web:///Assets/" doesn't work
}
}
}
I've tried different variations of returning BaseUrl of "ms-appx-web:///Assets/", "ms-appx-web:///", placing the files in the project root or in the “Assets” dir, nothing works.
As far as I can tell, this used to work on Windows Phone 8.0.
What about Device.OnPlatform?
var urlIos = #"somePicture.png";
var urlAndroid = #"somePicture.png";
var urlUWP = #"ms-appx-web:///Assets/somePicture.png";
htmlSource.Html = string.Format(#"<html><body><img src='{0}' /></body></html>", Device.OnPlatform(urlIos, urlAndroid, urlUWP));
As of Xamarin Forms 2.3.1, WebViewRenderer for Windows RT simply ignores the BaseUrl (https://github.com/xamarin/Xamarin.Forms/blob/2.3.1/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs#L26). However, there is a fix for this on the 2.3.2 branch: https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs

Master Detail Page on the right side using Xamarin.Forms

I've created a master detail page on the left side using Xamarin.Forms, how about creating the same for the right side?
Below is my sample code for the left slider menu;
public class App
{
static MasterDetailPage MDPage;
public static Page GetMainPage()
{
return MDPage = new MasterDetailPage {
Master = new ContentPage {
Title = "Master",
BackgroundColor = Color.Silver,
Icon = Device.OS == TargetPlatform.iOS ? "menu.png" : null,
Content = new StackLayout {
Padding = new Thickness(5, 50),
Children = { Link("A"), Link("B"), Link("C") }
},
},
Detail = new NavigationPage(new ContentPage {
Title = "A",
Content = new Label { Text = "A" }
}),
};
}
static Button Link(string name)
{
var button = new Button {
Text = name,
BackgroundColor = Color.FromRgb(0.9, 0.9, 0.9)
};
button.Clicked += delegate {
MDPage.Detail = new NavigationPage(new ContentPage {
Title = name,
Content = new Label { Text = name }
});
MDPage.IsPresented = false;
};
return button;
}
}
This does not exists in the Xamarin.Forms controls set, but you can create your own, with renderers for each platform.
You'll find the required information on http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/
Solution here
this is now supported in xamarin forms 3.0 and up, NO custom renderers
needed !! or third party libraries.
the trick is to force the layout to RTL,
while flow direction works, its hard to make the top nav bar to follow,
below is the solution for that problem, it works... let me know if you face any issues on older ios versions IOS8 and less.
xamarin forms RTL master details page with icon RTL/LTR hamburger
You can make it by ToolbarItems in Xamarin Forms
ToolbarItems will appear in right side.
You can find more info from the following links :
http://codeworks.it/blog/?p=232

Resources