Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用保存功能单击appbar后无法更新信息_C#_Windows Phone_Application Bar - Fatal编程技术网

C# 使用保存功能单击appbar后无法更新信息

C# 使用保存功能单击appbar后无法更新信息,c#,windows-phone,application-bar,C#,Windows Phone,Application Bar,我显示一个成员全名列表,当我选择一个成员时,它将转到详细信息页面。我直接在文本框中编辑信息,然后在应用程序栏中单击“保存”,但信息在编辑时没有更改…它仍然保持旧值 请帮帮我 我有这样的会员名单 <ListBox x:Name="Listmember" Height="500" SelectionChanged="Listmember_SelectionChanged" ItemsSource="{Binding Data}" > <ListBox.Ite

我显示一个成员全名列表,当我选择一个成员时,它将转到详细信息页面。我直接在文本框中编辑信息,然后在应用程序栏中单击“保存”,但信息在编辑时没有更改…它仍然保持旧值

请帮帮我

我有这样的会员名单

<ListBox x:Name="Listmember" Height="500" SelectionChanged="Listmember_SelectionChanged" ItemsSource="{Binding Data}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="466" Margin="0, 0, 0, 12">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Column="0"></Grid>
                        <StackPanel Grid.Column="1" >
                            <TextBlock FontSize="40"   Text="{Binding FullName}" Foreground="#FFEA0909" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>


                        </StackPanel>
                        <Grid Grid.Column="2">
                            <Button x:Name="Deletebutton" Height="60" Width="60" Click="deleteButton_Click" BorderBrush="{StaticResource TransparentBrush}">

                                <Image Source="/Assets/delete.dark.png" Visibility="{StaticResource PhoneDarkThemeVisibility}" Margin="-24"/>
                            </Button>
                        </Grid>
                    </Grid>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
在MemberDetails.xaml中,导航到:

<Grid Name="ListDetails" Grid.Row="0">
            <TextBlock Grid.Row="0" Name="fn" Text="FullName" FontSize="30" VerticalAlignment="Bottom"/>
            <TextBox Grid.Row="1" Name="fullnameTextBox" Text="{Binding FullName, Mode=TwoWay}" TextWrapping="Wrap"/>
            <TextBlock Grid.Row="2" Name="ad" Text="Address" FontSize="30" VerticalAlignment="Bottom"/>
            <TextBox Grid.Row="3" Name="addressTextBox" Text="{Binding Address, Mode=TwoWay}" TextWrapping="Wrap" />
 </Grid>
 <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton 
            IconUri="/Images/save.png" 
            Text="Save" 
            x:Name="SaveButton" 
            Click="Save_Click"/>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
和ViewModel中的类ViewDetails:

public class ViewDetails : INotifyPropertyChanged
{

    private string _fullname;

    public string FullName
    {
        get
        {
            return _fullname;
        }
        set
        {
            _fullname = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Fullname"));
            }
        }
    }

    private string _address;

    public string Address
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Address"));
            }
        }
    }

    public void UpdateDetails(Member abc)
    {
        abc.FullName = FullName;
        abc.Address = Address;


    }

    public void LoadDetails(Member abc)
    {
        FullName = abc.FullName;
        Address = abc.Address;
    }



     #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify the app that a property has changed.
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

处于绑定模式Twoway的TextBox在焦点丢失之前不会更新源

  • 若你们试着按下按钮,你们会发现按下按钮会 触发文本框的LostFocus事件,以便更新数据 并将处于正确的状态,然后才能在中执行代码 按钮

  • 如果您尝试按应用程序按钮,您会发现 LostFocus没有开火。因此,数据将不会更新,并将 处于旧状态

现在我们怎样才能克服这个问题呢

简单地说,您可以执行以下解决方案之一,在每次文本更改时更新源代码

  • 使用显式绑定结合OnTextChanged from

    显式:仅当调用 UpdateSource方法。当用户 离开文本框

    在xaml中

    <TextBox TextChanged="OnTextBoxTextChanged" Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
    
  • 你可以使用像这样的行为来倾听 到 text已更改事件并更新源。我更喜欢这个解决方案 添加到文本框很容易,无需在代码中添加更多行 在…的后面你所需要做的就是将这些行添加到你的文本框中

    <TextBox x:Name="tbx" Text="{Binding Name, Mode=TwoWay}">
    <i:Interaction.Behaviors>
    <local:UpdateSourceOnTextChangedBehavior />
    </i:Interaction.Behaviors>
    </TextBox>
    

  • 当你说..但什么都没发生时,你期望发生什么?@har07我的意思是信息在我编辑时没有更改你的意思是尽管已经从详细信息页面编辑并保存,但列表中的信息没有更改?您是否在成员类中实现了INotifyPropertyChanged?@har07是的。就这样。当我使用按钮而不是应用程序栏时…它工作得很好。我意识到,当我使用按钮时,事件处理程序是“私有void Save_Click(objectsender,RoutedEventArgs e)”,但在应用程序栏中,它只是EventArgs而不是RoutedEventArgs。。。我不知道为什么不同的方法争论很重要?我不这么认为。你试过调试吗?很难说问题到底出在哪里,你发布的代码看起来很好。如果这个项目还是一个小项目,你可以把它上传到某个地方,这样我们可以进一步提供帮助。。
    <TextBox TextChanged="OnTextBoxTextChanged" Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
    
    private void OnTextBoxTextChanged( object sender, TextChangedEventArgs e )
    {
      TextBox textBox = sender as TextBox;
      // Update the binding source
      BindingExpression bindingExpr = textBox.GetBindingExpression( TextBox.TextProperty );
      bindingExpr.UpdateSource();
    }
    
    <TextBox x:Name="tbx" Text="{Binding Name, Mode=TwoWay}">
    <i:Interaction.Behaviors>
    <local:UpdateSourceOnTextChangedBehavior />
    </i:Interaction.Behaviors>
    </TextBox>
    
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
     xmlns:local="name space of the behavior"