【问题标题】:Xaml C# Bind data works only the first timeXaml C# 绑定数据仅在第一次工作
【发布时间】:2013-10-28 10:24:39
【问题描述】:

我的问题是我在我的视图和我的数据之间使用绑定,但它只在第一次工作,如果我再次更新我的数据,我的视图不会更新。 (Windows 手机 8)

查看代码:

    <phone:LongListSelector x:Name="DataContextAction" DataContext="{Binding DataContextAction}" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="665" Margin="10,10,0,0" VerticalAlignment="Top" Width="471">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="{StaticResource PhoneMargin}">
                    <TextBlock Text="{Binding HardwareId}" Style="{StaticResource PhoneTextLargeStyle}"/>
                    <TextBlock Text="{Binding Type}" Style="{StaticResource PhoneTextNormalStyle}"/>
                    <TextBlock Text="{Binding Status}" Style="{StaticResource PhoneTextNormalStyle}"/>
                </StackPanel>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>

xaml.cs 页面:

public DevicePage()
{
    InitializeComponent();

    // Display message waiting before make the http query.
    txtMessage.Foreground = new SolidColorBrush(Colors.Orange);
    txtMessage.Text = "Loading...";

    // Initialize the view data binding.
    InitializeAsynch();

    // Start new thread in background.
    var refreshViewBackground = new System.Threading.Thread(RefreshViewBackground);
    refreshViewBackground.Start();
}

private async void InitializeAsynch()
{
    // TODO BAD CODE but I don't understand (and I don't have enough time) how make it better. http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html
    DataContext = await UserSession.RefreshLastLocationDevices();
    // If I do that now with empty session, the view will be empty.
    //DataContextAction.ItemsSource = UserSession.Actions;
    txtMessage.Text = "";
}

当我开始一个新动作时,我会运行一个新线程,该线程将在动作完成时更新会话。 (与之前的代码在同一个文件中)

private void StartListenNewAction(DataAction action, DataDevice device)
{
    // Update the actions list;
    try
    {
         DataContextAction.ItemsSource = UserSession.Actions;
    }
    catch (Exception e)
    {
        Debug.WriteLine("Unable to refresh, another thread has updated the action list. " + e.Message);
    }

    // Start new thread in background for this specific action.
    ParameterizedThreadStart refreshActionBackground = new  ParameterizedThreadStart(RefreshActionBackground);
    Thread thread = new Thread(refreshActionBackground);
    Thread.Start(new object[] { action, device });
}

但在这里,这只是第一次。我的观点只更新一次。 谢谢大家的帮助,没看懂,我用的是另外一个longListSelector,使用session没有问题,但是我直接用的是{Binding},不是名字。

<phone:LongListSelector x:Name="listDevices" ItemsSource="{Binding}" ...

所以,我不明白为什么它在这里不起作用。

编辑: 如题:

// Refresh only one action in background.
public async void RefreshActionBackground(object args)
{
    bool pending = true;

    DataAction action = (DataAction)((object[])args)[0];
    DataDevice device = (DataDevice)((object[])args)[1];

    while (pending)
    {
        // Sleep some time.
        System.Threading.Thread.Sleep(device.AskingFrequencyActive * 1000);

        // Get the action from the web service.
        List<Param> parameters = new List<Param>();
        parameters.Add(new Param("email", UserSession.Email));
        parameters.Add(new Param("password", UserSession.Password));
        parameters.Add(new Param("hardwareId", device.HardwareId));// Mandatory.
        parameters.Add(new Param("id", action.Id));
        parameters.Add(new Param("limit", "1"));

        // Send the request.
        IDictionary<string, object> response = await WebService.Send("action", "find", parameters);

        if ((bool)response["status"])
        {
            // Get objects from the response.
            Dictionary<string, object> data = (Dictionary<string, object>)response["data"];
            // We got an array, convert to array.
            JsonArray actionsArray = (JsonArray)data["action"];

            // Update the session.
            actionsArray.ForEach(delegate(dynamic actionJson)
            {
                // Update the action from the session.
                UserSession.Actions.Where(s => (string)s.Id == (string)actionJson["id"]).First().Status = (string)actionJson["status"];

                // Get the action from the session.
                DataAction actionSession = UserSession.Actions.Where(s => s.Id == action.Id).First();

                // Compare to the session which could be updated since the last time.
                if (actionSession.Status == "Executed")
                {
                    pending = false;
                    // Update UI thread.
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        // Update the actions list;
                        try
                        {
                            DataContextAction.ItemsSource = UserSession.Actions;
                        }catch(Exception e){
                            Debug.WriteLine("Unable to refresh, another thread has updated the action list. " + e.Message);
                        }

                        // Update UI interface.
                        txtMessage.Foreground = new SolidColorBrush(Colors.Green);
                        this.txtMessage.Text = "Action executed: " + action.Type;
                    });
                }
            });

        }
        else
        {
            Debug.WriteLine((string)response["message"]);
        }
    }
}

【问题讨论】:

  • 我愿意让你阅读这个 - 关于 INotifyPropertyChanged codeproject.com/Articles/41817/…
  • RefreshActionBackground' and RefreshViewBackground` 在哪里定义?可以展示一下吗?
  • 主帖已更新。添加了 RefreshActionBackground 函数。
  • 你能看看这个video about using async void方法吗。
  • 我会的,但是已经结束了,项目完成了。谢谢。

标签: c# xaml binding windows-phone-8


【解决方案1】:

正如我所说,项目已经结束,但我想出了如何让它工作,实际上是一种糟糕的方式,绝对是疯狂的。

这很简单,如果我的列表视图包含多个设备,它就可以工作。如果只有一个,它就不起作用。完全不知道发生了什么,我也不再研究它了,所以这是一个诡计,可能会发生在其他人身上。

项目托管在 git 上,这里:https://bitbucket.org/Vadorequest/trip-analyzer-server-node.js/overview

感谢您的帮助。

【讨论】:

    【解决方案2】:

    我认为您不应该使用新线程。在系统用完它们之前,您基本上是无缘无故地要求新线程。考虑改用Tasks。您确定您在第二次更新用户界面时仍然在正确的线程上吗?

    【讨论】:

    • 在 RefreshActionBackground() 函数中,我使用 Deployment.Current.Dispatcher.BeginInvoke 执行更新 UI 线程的代码,所以我认为这很好。在 StartListenNewAction() 函数中,我在 UI 线程中。反正我会试试的。我不了解任务,我将它们用于等待呼叫,但仅此而已。我真的没有时间更新应用程序,我对 WP8 视图一无所知,有 24 小时...
    猜你喜欢
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多