我无法在示例项目中实现DisplayPrompt

时间:2019-12-04 09:23:43

标签: xamarin xamarin.forms

我引用Xamarin.Forms文档。链接添加如下 enter link description here 请任何人有解决方案请帮助我 * DisplayPrompt 实施

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.";
        }
    }
}
}

遇到错误:

  

1 .CS0103当前上下文中不存在名称'DisplayPromptAsync'

     

2 。严重性代码说明项目文件行抑制状态抑制状态错误“ FilterAssemblies”任务失败   不料。 System.IO.FileNotFoundException:找不到文件   'C:\ Users \ Sharjas K   M \ source \ repos \ tEST \ tEST \ tEST \ bin \ Debug \ netstandard2.0 \ tEST.dll”。文件   名称:'C:\ Users \ Sharjas K   M \ source \ repos \ tEST \ tEST \ tEST \ bin \ Debug \ netstandard2.0 \ tEST.dll位于   System.IO .__ Error.WinIOError(Int32 errorCode,字符串mayFullPath)
  在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess   访问,Int32权限,布尔useRights,FileShare共享,Int32   bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串   MSGPATH,布尔bFromProxy,布尔useLongPath,布尔checkHost)
  在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess   访问,FileShare共享)   Xamarin.Android.Tasks.FilterAssemblies.Execute()位于   Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()   在   Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()

3 个答案:

答案 0 :(得分:1)

注意:DisplayPromptAsync是Xamarin.Forms 4.3.0的功能

如果要显示alert

bool result= await DisplayAlert ("Alert", "Continue..", "Yes", "No");

如果您想接受用户的输入

string result = await DisplayPromptAsync("Question 1", "What's your name?");

result处返回用户输入的值。

答案 1 :(得分:1)

DisplayPrompt 在XF 4.3之后可用。因此,您可以首先在Nuget中更新Xamarin.Forms的版本。然后删除共享项目中的文件夹binobj。然后清理并重建它。

enter image description here

注意:当前仅在iOS和Android上实现DisplayPromptAsync方法(因此在UWP中您无法调用它)。

答案 2 :(得分:0)

我使Xamarin.Forms没有更新的错误。 DisplayPromptAsync是最近添加的。您可能需要更新Xamarin.Forms 版本4.3.0.991221 NuGet程序包。UWP上尚未实现DisplayPromptAsync。