I refer Xamarin.Forms docs. the link add below
enter link description here
Please anyone has a solution Please help me
* DisplayPrompt implementation
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="tEST.MainPage">
<StackLayout Margin="20,35,20,20">
<Label Text="Display Prompt"
FontSize="Large"
HorizontalOptions="Center" />
<Button Text="Question 1"
Clicked="OnQuestion1ButtonClicked" />
<Label x:Name="question1ResultLabel" />
<Button Text="Question 2"
Clicked="OnQuestion2ButtonClicked" />
<Label x:Name="question2ResultLabel" />
</StackLayout>
</ContentPage>
C#
using System;
using Xamarin.Forms;
namespace tEST
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
async void OnQuestion1ButtonClicked(object sender, EventArgs e)
{
string result = await DisplayPromptAsync("Question 1", "What's your name?");
if (!string.IsNullOrWhiteSpace(result))
{
question1ResultLabel.Text = $"Hello {result}.";
}
}
async void OnQuestion2ButtonClicked(object sender, EventArgs e)
{
string result = await DisplayPromptAsync("Question 2", "What's 5 + 5?", maxLength: 2, keyboard: Keyboard.Numeric);
if (!string.IsNullOrWhiteSpace(result))
{
int number = Convert.ToInt32(result);
question2ResultLabel.Text = number == 10 ? "Correct." : "Incorrect.";
}
}
}
}
Getting errors:
1.CS0103 The name 'DisplayPromptAsync' does not exist in the current context
2.Severity Code Description Project File Line Suppression State Suppression State Error The "FilterAssemblies" task failed
unexpectedly. System.IO.FileNotFoundException: Could not find file
'C:\Users\Sharjas K
M\source\repos\tEST\tEST\tEST\bin\Debug\netstandard2.0\tEST.dll'. File
name: 'C:\Users\Sharjas K
M\source\repos\tEST\tEST\tEST\bin\Debug\netstandard2.0\tEST.dll' at
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32
bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String
msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share) at
Xamarin.Android.Tasks.FilterAssemblies.Execute() at
Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at
Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()
Note: DisplayPromptAsync is feature of Xamarin.Forms 4.3.0
If you want to show alert
bool result= await DisplayAlert ("Alert", "Continue..", "Yes", "No");
If you want to take input from user
string result = await DisplayPromptAsync("Question 1", "What's your name?");
Where result you return you value entered by user.
DisplayPrompt is available after XF 4.3 . So you could firstly update the version of Xamarin.Forms in Nuget . Then delete the folder bin and obj in your share project. Then clean and rebuild it.
Note: The DisplayPromptAsync method is currently only implemented on iOS and Android(So in UWP you could not call it).
The mistake I made Xamarin.Forms not updated. DisplayPromptAsync is a recent addition. You may need to update the Xamarin.Forms version 4.3.0.991221 NuGet package.DisplayPromptAsync is not yet implemented on UWP.
Related
I have a bunch of Entrys and Picker and I want the focus to automatically change to the next Entry/Picker.
Im doing that by calling the Focus() method on the Entry/Picker I wanna focus to. If my current focus is on an Entry and my next element is an Entry or a Picker it changes the focus and opens the Keyboard/Pickerdialog as it's supposed to. If my current focus is on a Picker and the next element is an Entry then the Entry gets focused, as supposed to, but the keyboard does not open.
The focus() method is called in the Pickers SelectedIndexChanged event.
How can I fix that?
xaml code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="ABM.Ablesegeraet.Views.TourenLadenPage"
Title="Touren laden">
<ContentPage.Content>
<StackLayout>
<Label Grid.Row="0" Grid.Column="0" Margin="30,10,0,0" FontSize="Medium" Text="Mandant:"/>
<Picker x:Name="MandantenPicker" ItemDisplayBinding="{Binding Ort}" Title="Mandant" Grid.Row="0" Grid.Column="1" SelectedIndexChanged="MandantPicker_SelectedIndexChanged"/>
<Entry Grid.Row="1" x:Name="test"/>
</StackLayout>
</ContentPage.Content>
SelectedIndexcChanged:
private void MandantPicker_SelectedIndexChanged(object sender, EventArgs e)
{
test.Focus();
}
Picker did not lose focus during the SelectedIndexChanged method of Picker, and then if you set Entry.Focus, it will cause a display error.
The solution is that Picker loses focus after the SelectedIndexChanged method runs, and then sets Entry.Focus to display it correctly.
Use Task to open a new thread to complete this operation.
Here is the backgroundcode:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void picker_SelectedIndexChanged(object sender, EventArgs e)
{
var a = picker.IsFocused;
string res = (sender as Picker).SelectedItem.ToString();
Task.Run(()=> myTask(res));//Create and start the thread
}
private void myTask(string res)
{
Thread.Sleep(300);
var a = picker.IsFocused;
if (res == "Test1")
{
entry1.Focus();
}
if (res == "Test2")
{
entry2.Focus();
}
}
}
Description
I have created simple Xamarin.Forms sample. In it, I have added an Image and loaded the image as a stream through ImageSource.FromSource() method, then removed and add it to the layout.
Xamarin.Forms UWP The image will be disappeared.
Xamarin.Forms Android Cannot access closed stream exception throwing.
Xamarin.Forms iOS Didn't check yet.
Please find the code snippet below
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ImageException.MainPage">
<ContentPage.Content>
<Grid x:Name="MainGrid">
<Image x:Name="image" />
<Button Text="Click_Me"
Clicked="Button_Clicked"
VerticalOptions="End" />
</Grid>
</ContentPage.Content>
</ContentPage>
C#:
namespace ImageException
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var assembly = typeof(MainPage).Assembly;
string path = "ImageException";
var stream = assembly.GetManifestResourceStream($"{path}.Image.Image.png");
image.Source = ImageSource.FromStream(() => stream);
}
private void Button_Clicked(object sender, EventArgs e)
{
MainGrid.Children.Remove(image);
MainGrid.Children.Add(image);
}
}
}
Please get the complete sample from below link https://github.com/VigneshRameshh/XamarinFormsImageException
Can anyone suggest how to retain the image after removed and added from the layout?
Thanks in advance,
Vignesh Ramesh.
I checked your code and could reproduce your exception.
If you want to fix this, you could use the code like below.
Stream stream;
Assembly assembly;
string path;
public Page18()
{
InitializeComponent();
assembly = typeof(MainPage).Assembly;
path = "App14";
stream = assembly.GetManifestResourceStream($"{path}.Image.beach.jpg");
image.Source = ImageSource.FromStream(() => stream);
}
private void Button_Clicked(object sender, EventArgs e)
{
stream = assembly.GetManifestResourceStream($"{path}.Image.beach.jpg");
MainGrid.Children.Remove(image);
MainGrid.Children.Add(image);
}
I am trying for saving some value in the editor after clicking on done on the keyboard for that I used the Completed event for the editor but this is calling while tapping on everywhere in the view like the Unfocused event. How to avoid that this?
<controls:CustomEditor Keyboard="Default"
ReturnKeyType="Next"
TextChanged="Comment_Changed"
Completed="OnDoneClicked"
VerticalOptions="StartAndExpand"
HorizontalOptions="FillAndExpand"
Text="{Binding QuestionComment}">
<controls:CustomEditor.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double"
iOS="12"
Android="12"
WinPhone="30" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double"
iOS="13"
Android="13"
WinPhone="40" />
</OnIdiom.Tablet>
</OnIdiom>
</controls:CustomEditor.FontSize>
</controls:CustomEditor>
CustomEditorClass is as following:
public const string ReturnKeyPropertyName = "ReturnKeyType";
public CustomEditor() { }
public static readonly BindableProperty ReturnKeyTypeProperty = BindableProperty.Create(
propertyName: ReturnKeyPropertyName,
returnType: typeof(ReturnKeyTypes),
declaringType: typeof(CustomEditor),
defaultValue: ReturnKeyTypes.Done );
public ReturnKeyTypes ReturnKeyType
{
get { return (ReturnKeyTypes)GetValue(ReturnKeyTypeProperty); }
set { SetValue(ReturnKeyTypeProperty, value); }
}
public enum ReturnKeyTypes : int
{
Default,
Go,
Google,
Join,
Next,
Route,
Search,
Send,
Yahoo,
Done,
EmergencyCall,
Continue
}
And the event is as following:
private async void OnDoneClicked(object sender, EventArgs e)
{
//some logic
}
Please see the documentation, this is the intended behavior
iOS (Unfocusing the editor or pressing "Done" triggers the event). Android / Windows Phone (Unfocusing the Editor triggers the event)
I've checked some messenger apps (iOS) and they don't have a done button on the keyboard at all, they provide a send button besides the entry control.
Depending on what you try to achieve, I'd ditch the done button on the keyboard altogether and rely on some sort of view on the UI, that is used for that purpose. This is platform dependent behavior anyway, Android does not support it at all, AFAIK. In order to get rid of the done button, you'll have to implement a custom renderer and set the InputAccessoryView of the native control to null (see here)
[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace ProjectName.iOS
{
public class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.Control.InputAccessoryView = null;
}
}
}
If you really want a button to send, you could create a derivative of Editor with a custom renderer, that creates a InputAccessoryView and raises an SendPressed event on your custom Editor when the InputAccessoryView is pressed. But remember, this is not possible for Android.
on Button click file picker window not open in xamarin forms-
I am using Xam.Plugin.FilePicker
Here is my button event code-
async void FilePickerEvent(object sender, EventArgs e)
{
try
{
FileData filedata = new FileData();
filedata = await CrossFilePicker.Current.PickFile();
byte[] data = filedata.DataArray;
string name = filedata.FileName;
foreach(byte b in filedata.DataArray)
{
string attachment = b.ToString();
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
I tried this plugin on Android device and it works well. I installed the plugin in all projects and then added these permissions to AndroidManifest.xml file as it is in the documentation.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then in my MainPage.xaml I created a button
<Button Text="Open File" Clicked="Button_Clicked" />
And in my MainPage.xaml.cs created event
private async void Button_Clicked(object sender, EventArgs e)
{
var file = await CrossFilePicker.Current.PickFile();
}
I picked the file and retrieve the property FileName and DataArray without any problem.
There is a thread on xamarin forum or there is a issue list on their github.
There is a newer forked github project that received quite some fixes, e.g. for Android picking from download folder, OneDrive or Google Drive storage. The project is here:
https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows
(note: I'm one of the contributors).
You just have to change the NuGet package name to Xamarin.Plugin.FilePicker, the API is the same.
I'm developing windows universal app(XAML,C#) on phone, and am enabling accessibility for Narrator. Does anybody know how to get narrator automatically to read page title when a page is opened?
I tried setting automationproperties.name in page but didn't work:
<Page
x:Class="xxxxxx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AutomationProperties.Name="Page title to be read"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
The features of the Narrator for UWP are applied when you select a control in a list, or editing a textbox. If you want to read content when the app is opened you should use the SpeechSynthesizer API, which is really easy to implement:
1.- In XAML add a Media Element
<MediaElement x:Name="MediaElement"/>
2.-Then in the code behind of the page:
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ReadTitle();
}
private async void ReadTitle()
{
var voice = SpeechSynthesizer.AllVoices.First();
SpeechSynthesizer reader = new SpeechSynthesizer() { Voice = voice };
var text= this.GetValue(AutomationProperties.NameProperty) as String;
await reader.SynthesizeTextToStreamAsync(text);
MediaElement.SetSource(stream, stream.ContentType);
MediaElement.Play();
}
You can read everything you want passing the string to the reader.
You need to make view-able by narrator. I don't believe you can declare the Name property within the Page Class. Try something like this within the content of your Page:
<HyperlinkButton
NavigateUri="www.bing.com"
AutomationProperties.AutomationID="bing url" //Not Required to work
AutomationProperties.Name="Go to the Bing Homepage"//Narrator will read this
<StackPanel>
<TextBlock Text="Bing Dot Com" />
<TextBlock Text="www.bing.com" />
<StackPanel>
</HyperlinkButton>
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.automation.peers.accessibilityview
EDIT: You may also need to programmatically set focus on the item for this to work