在运行时禁用/启用应用程序栏按钮,事件文本已更改(Windows Phone) [英] Disable/Enable applicationbar Button in runtime with event textchanged (Windows Phone)

查看:54
本文介绍了在运行时禁用/启用应用程序栏按钮,事件文本已更改(Windows Phone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分代码是事件TextChanged,用于启用应用程序栏中的按钮.

In this part of the code is the event TextChanged to enable the button in te applicationbar.

C#:

private void Textbox_TextChanged(object sender, EventArgs e)
{
    ApplicationBarIconButton btn_guardar = ApplicationBar.Buttons[0] as applicationBarIconButton;

    if (!string.IsNullOrEmpty(txt_nom_usuario.Text) && !string.IsNullOrEmpty(txt_edad_usuario.Text) && !string.IsNullOrEmpty(txt_peso_usuario.Text))
    {
        btn_guardar.IsEnabled = true;
    }
    else
        btn_guardar.IsEnabled = false; 
}

XAML:

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar Mode="Default" IsVisible="True">
        <shell:ApplicationBarIconButton x:Name="btn_guardar" IconUri="/icons/appbar.save.rest.png" Text="Guardar" Click="btn_guardar_Click" IsEnabled="False" />
        <shell:ApplicationBarIconButton x:Name="btn_atras" IconUri="/icons/appbar.back.rest.png" Text="Atrás" Click="btn_atras_Click" />
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

<TextBlock x:Name="lbl_ingresanombre" Height="39" Margin="60,28,0,0" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Width="248" FontSize="29.333" FontFamily="{StaticResource Helvetica}"><Run Text="Ingresa "/><Run Text="tu nombre"/></TextBlock>
<TextBox x:Name="txt_nom_usuario" Height="63" Margin="47,58,69,0" TextWrapping="Wrap" Text="&#xa;" FontSize="21.333" VerticalAlignment="Top" IsEnabled="True" />

<TextBlock x:Name="lbl_edad" Height="38" Margin="60,117,0,0" TextWrapping="Wrap" Text="Ingresa tu edad" VerticalAlignment="Top" FontSize="29.333" HorizontalAlignment="Left" FontFamily="{StaticResource Helvetica}"/>
<TextBox x:Name="txt_edad_usuario" InputScope="TelephoneLocalNumber" Height="63" TextWrapping="Wrap" Text="&#xa;" FontSize="21.333" Margin="47,147,69,0" VerticalAlignment="Top" MaxLength="3" />

<TextBlock x:Name="lbl_peso" Height="42" Margin="60,0,0,178" TextWrapping="Wrap" Text="Peso" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="74" FontSize="29.333" d:LayoutOverrides="HorizontalAlignment" FontFamily="{StaticResource Helvetica}"/>
<TextBox x:Name="txt_peso_usuario" InputScope="TelephoneLocalNumber" Margin="47,0,69,125" TextWrapping="Wrap" Text="&#xa;" FontSize="21.333" Height="63" VerticalAlignment="Bottom"/>

推荐答案

应用程序栏在 XAML 中设置时不支持某些基本功能.您必须通过代码创建栏和按钮和/或菜单项.

The application bar doesn't support some basic features when it is set in XAML. You'll have to create the bar and buttons and/or menu items through code.

这是一个如何创建栏并向其添加控件的示例.然后可以稍后从代码访问控件:

Here's an example how you can create the bar and add controls to it. The controls can then be accessed later from code:

//button
var appBarButton = new ApplicationBarIconButton
{
    IconUri = new Uri("/Images/YourImage.png", UriKind.Relative),
    Text = "click me"
};
appBarButton.Click += new EventHandler(appBarButton_Click);

//menu item
ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem
{
    Text = "a menu item"
}
appBarMenuItem.Click += new EventHandler(appBarMenuItem_Click);

//application bar

//Note that this is not a variable declaration
//'ApplicationBar' is a property of 'PhoneApplicationPage'

ApplicationBar = new ApplicationBar();
ApplicationBar.Buttons.Add(appBarButton);
ApplicationBar.MenuItems.Add(appBarMenuItem);

//the events
private void appBarButton_Click(object sender, EventArgs e)
{
}

private void appBarMenuItem_Click(object sender, EventArgs e)
{
}

完成所有这些后,您就通过代码创建了自己的 ApplicationBar.现在您可以通过代码更改属性,如下所示:

When all this is done, you've created your own ApplicationBar through code. Now you can change the properties from code, like this:

var theButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];

if(someCondition)
{
    theButton.IsEnabled = true;
}
else
{
    theButton.IsEnabled = false;
}

//or shorter:
theButton.IsEnabled = someCondition

这只是一个例子.在 TextChanged 事件中,您还可以访问 ApplicationBar 控件.在这些事件中,您可以放置​​上面的代码来更改 ApplicationBarButton.希望这可以为您解决问题!更多阅读和信息:

This is just an example. In the TextChanged events you can also access the ApplicationBar controls. In these events you can place above code to change the ApplicationBarButton. Hope this clears things up for you! More reading and info:

这篇关于在运行时禁用/启用应用程序栏按钮,事件文本已更改(Windows Phone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆