public void LoadSettings() { this.UserName = BsaContext.GetUserName(); this.Url = $"http://{BsaContext.GetURL()}:8081"; this.Role = BsaContext.GetUserRole(); this.IP = AddressResolving.GetIp4AddressesString(); }
// Problem: Can't use a loop to create the dropdown menu buttons for the AquireServerButton, because // Command = new RelayCommand(() => this.ChangeToServer(addresses[i].ToString()) // can't be executed, due to access to modified closure (the variable i) within lambda expression. // Normally you can create a new local variable (var i1 = i) and use this local variable within the lambda expression // -> no access to modified closure anymore. // But this doesn't work here, too. // You can't specify a index of the addresses list within the "Command" - lambda expression here. // It has to be a constant value. // // This method is a pseudo-workaround (very, very ugly). // Wasn't able to find another solution at this point. // It stops working, once AddressResolving.GetIpv4Addresses() returns more than 5 elements. // ToDo find another solution to add the ip addresses to the AquireServerButton, with some working loop private void AddAquireServerButtonDropdownElements() { var addresses = AddressResolving.GetIp4Addresses().ToList(); var dropdownButtons = new List <ShellCommandBarButton>(); // This does NOT work! //foreach (var ipAddress in addresses) //{ // this.AcquireServerButton.Items.Add( // new ShellCommandBarButton // { // ImageUri = // new Uri("pack://application:,,/Resources/Images/ShellIcons/versions.scale-150.png"), // Label = ipAddress.ToString(), // Command = new RelayCommand(() => this.ChangeToServer(ipAddress.ToString())) // }); //} // The following code works, but it's ugly // ToDo Fix that, create a better code try { if (addresses.Count >= 1) { dropdownButtons.Add(new ShellCommandBarButton { ImageUri = new Uri("pack://application:,,/Resources/Images/ShellIcons/versions.scale-150.png"), Label = addresses[0].ToString(), Command = new RelayCommand(() => this.ChangeToServer(AddressResolving.GetIp4Addresses().ToList()[0].ToString())) }); } if (addresses.Count >= 2) { dropdownButtons.Add(new ShellCommandBarButton { ImageUri = new Uri("pack://application:,,/Resources/Images/ShellIcons/versions.scale-150.png"), Label = addresses[1].ToString(), Command = new RelayCommand(() => this.ChangeToServer(AddressResolving.GetIp4Addresses().ToList()[1].ToString())) }); } if (addresses.Count >= 3) { dropdownButtons.Add(new ShellCommandBarButton { ImageUri = new Uri("pack://application:,,/Resources/Images/ShellIcons/versions.scale-150.png"), Label = addresses[2].ToString(), Command = new RelayCommand(() => this.ChangeToServer(AddressResolving.GetIp4Addresses().ToList()[2].ToString())) }); } if (addresses.Count >= 4) { dropdownButtons.Add(new ShellCommandBarButton { ImageUri = new Uri("pack://application:,,/Resources/Images/ShellIcons/versions.scale-150.png"), Label = addresses[3].ToString(), Command = new RelayCommand(() => this.ChangeToServer(AddressResolving.GetIp4Addresses().ToList()[3].ToString())) }); } if (addresses.Count >= 5) { dropdownButtons.Add(new ShellCommandBarButton { ImageUri = new Uri("pack://application:,,/Resources/Images/ShellIcons/versions.scale-150.png"), Label = addresses[4].ToString(), Command = new RelayCommand(() => this.ChangeToServer(AddressResolving.GetIp4Addresses().ToList()[4].ToString())) }); } // Add buttons to dropdown menu this.AcquireServerButton.Items.AddRange(dropdownButtons); } catch (Exception) { // Do nothing. // Just wanted to create some exception handling when dealing with about 3 billion indices. } }