简体   繁体   中英

UWP C# - Databinding and Updating of Controls

Let me preface this by saying I'm writing my first UWP app. This app will pull some JSON values from servers and aggregate the results. I'd like to give users the ability to add and remove servers. I'm storing the info in the application data folder using the LocalObjectStorageHelper from the Community Tookkit. Here is the class that handles that:

class ServerSettingsStore
{

    LocalObjectStorageHelper helper = new LocalObjectStorageHelper();
    private string serverNumber;

    public ServerSettingsStore(string serverNum)
    {
        serverNumber = serverNum;

    }

    public void Save_Settings(ServerInfo serverInfo)
    {

        helper.Save(serverNumber, serverInfo);

    }

    public ServerInfo Retrive_Settings()
    {
        var result = helper.Read<ServerInfo>(serverNumber);

        return result;

    }

Here is the class that handles the ServerInfo:

 public class ServerInfo
{

    public string ServerName { get; set; }
    public string ServerAddress { get; set; }
    public string ServerAuthKey { get; set; }


    public string HostNameSummary
    {
        get
        {
            return this.ServerName;
        }
    }
}

Now, on the UWP Page, I'm binding Data to a list box successfully:

XAML Code:

<ListBox x:Name="ServerList" ItemsSource="{x:Bind ViewModel.ServerList, Mode=OneWay}" SelectionChanged="ServerList_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate x:DataType="local:ServerInfo">
                            <StackPanel Orientation="Horizontal">
                                <StackPanel>
                            <TextBlock Text="{x:Bind ServerName}" FontWeight="Bold"/>
                            <TextBlock Text="{x:Bind ServerAddress}"  Margin="10,0,0,0"/>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Here is my ViewModel:

public class ListViewModel
{

    private ObservableCollection<ServerInfo> serverlist = new ObservableCollection<ServerInfo>();
    public ObservableCollection<ServerInfo> ServerList { get { return this.serverlist; } }

    public ListViewModel()
    {

        for (var i = 1; i <= Convert.ToInt32(new ServerCount().NumberOfServers); i++)
        {
            var settingsstore = new SettingsStore(i.ToString());
            var serverinfo = new ServerInfo();
            serverinfo = settingsstore.Retrive_Settings();
            this.serverlist.Add(new ServerInfo() { ServerName = serverinfo.ServerName, ServerAddress = serverinfo.ServerAddress, ServerAuthKey = serverinfo.ServerAuthKey });

        }
        
    }

}

Now comes the challenge. I have a SplitView panel that allows a user to modify the Server Name and IP Address, and saves it back the OjectStorageHelper. All of that works, but the data binding of the listview does not get updated with the new values. What is the best way to refresh this information? Am I breaking the data out into too many classes?

Based on the comments, it seems like when you are writing to the ObjectStorageHelper you aren't adding the new item(s) to the ObservableCollection property so your ListView is not updating as expected.

You will need to add that item to the collection once the save/write operation is successful and your ListView will update properly (your x:bind in the xaml fragment you shared looks ok).

Now, it seems like the code that works with ObjectStorageHelper is in a different ViewModel. I don't know how your code is structured, but you can either bring that code into the same ViewModel above and then update the ObservableCollection or possibly use Events/Notifications that are fired when the save operation is completed successfully and then a handler will add the item to the collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM