在Xamarin(visual studio)PCL项目中的视图之间导航

时间:2017-03-29 09:34:06

标签: c# forms button xamarin onclick

我找不到在按钮上实现onClick事件的方法,该按钮允许应用程序在登录和第二个视图之间导航。

我该怎么做?

这就是我做的事情

我在LoginViewModel.cs文件中创建了一个方法,该方法应该将我重定向到第二个视图。

 class LoginViewModel
{
 private async Task SecondView_Click()
        {
            App.Current.MainPage = new NavigationPage(new SecondView());
        }
}

然后我在Login.cs中定义了一个BindingContext

 public partial class Login : ContentPage
    {
        public Login()
        {
            InitializeComponent();
            BindingContext = new LoginViewModel();
        }
    }

然后我在Login.xaml中定义了一个具有绑定命令属性

的按钮
 <StackLayout
        VerticalOptions="CenterAndExpand">
        <Entry StyleId="UsernameEntry"
               Placeholder="Username"
               Text="{Binding Username}" />
        <Entry StyleId="PasswordEntry"
               Placeholder="password"
               Text="{Binding Password}" />
        <Button 
            StyleId="btn_connexion"
            Text="Connexion"
            Command="{Binding connexion}" />
        <Button 
            StyleId="btn_testSecondView"
            Text="Test 2nd View"
                Command="{Binding SecondView_Click}"></Button>
    </StackLayout>

2 个答案:

答案 0 :(得分:1)

这对我有用

第1页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="TestRelativeLayout.MyPage1"
             Title="TabbedPage">
             <StackLayout>
                <Button Clicked="Handle_Clicked" Text = "Press">

        </Button>
    </StackLayout>
</ContentPage>

第1页XAML.CS

using Xamarin.Forms;

namespace TestRelativeLayout
{
    public partial class MyPage1 : ContentPage
    {
        public MyPage1()
        {
            InitializeComponent();
        }

        public void Handle_Clicked(object sender, System.EventArgs e)
        {
            Application.Current.MainPage = new NavigationPage(new MyPage2());
        }
    }
}

尝试删除

private async Task

并使用

void

private async Task SecondView_Click()
        {
            App.Current.MainPage = new NavigationPage(new SecondView());
        }

答案 1 :(得分:0)

这就是我所做的。

我发现有一个&#34;点击了#34;该提示上的属性是一个带有&#34;新事件处理程序&#34;。

的intellisense下拉列表
<StackLayout
        VerticalOptions="CenterAndExpand">
        <Entry StyleId="UsernameEntry"
               Placeholder="Username"
               Text="{Binding Username}" />
        <Entry StyleId="PasswordEntry"
               Placeholder="password"
               Text="{Binding Password}" />
        <Button 
            StyleId="btn_connexion"
            Text="Connexion"
            Clicked="connexion" />
        <Button 
            StyleId="btn_testSecondView"
            Text="Test 2nd View"
                Clicked="SecondView_Click"></Button>
    </StackLayout>

一旦我这样做,它就会在&#34; Login.xaml.cs&#34;背后的代码中创建一个方法。

从那里我只需粘贴导航方法就可以了

private async Task SecondView_Click()
        {
            App.Current.MainPage = new NavigationPage(new SecondView());
        }

事实上,它是一个PCL项目,因此很难找到正确的信息,因为您在互联网上找到的所有内容都涉及ios / android解决方案,而不是便携式解决方案。

相关问题