У меня есть следующая проблема, в которой я пытаюсь привязать данные внутри списка модели к моему файлу xaml. Я хочу привязать список IOS-датчиков к файлу xaml в некоторых текстовых метках.
Модель: класс IOModule {
public IOModule(string Name, string Type, string Version, string Location, string Desc)
{
this.Name = Name;
this.Type = Type;
this.Version = Version;
this.Serial = Location;
this.Status = Desc;
this.list = new List<IOSensor>();
}
public void addSensorInput(IOSensor sensor)
{
list.Add(sensor);
}
[JsonProperty("Name")]
public String Name { get; set; }
[JsonProperty("Type")]
public String Type { get; set; }
[JsonProperty("Version")]
public String Version { get; set; }
[JsonProperty("Serial")]
public String Serial { get; set; }
[JsonProperty("Status")]
public String Status { get; set; }
public List<IOSensor> list { get; }
public List<String> getIDs
{
get
{
List<string> list1 = new List<string>();
foreach (IOSensor sens in list)
{
list1.Add(sens.ID);
}
return list1;
}
}
}
public class IOSensor
{
public IOSensor(String ID, String Type, String GasComp, String MeasureType, String value)
{
this.ID = ID;
this.IOType = Type;
this.GasComp = GasComp;
this.MeasurementType = MeasureType;
this.Value = value;
}
[JsonProperty("ID")]
public String ID { get; set; }
[JsonProperty("IOType")]
public String IOType { get; set; }
[JsonProperty("GasComp")]
public String GasComp { get; set; }
[JsonProperty("MeasurementType")]
public String MeasurementType { get; set; }
[JsonProperty("Value")]
public String Value { get; set; }
}
ViewModel:
class IoModulesViewModels : BaseViewModel
{
public Item item;
public ObservableCollection<IOModule> modlists;
public IoModulesViewModels(Item item)
{
this.item = item;
this.modlists= new ObservableCollection<IOModule>();
//sendReq();
IOModule mod1 = new IOModule("1", "2", "3", "4", "5");
mod1.addSensorInput(new IOSensor("44", "55", "66", "77", "88"));
mod1.addSensorInput(new IOSensor("444", "545", "646", "747", "848"));
modlists.Add(mod1);
modlists.Add(mod1);
}
}
Xaml:
public partial class IoModulesPage : ContentPage
{
IoModulesViewModels ioModModels;
public IoModulesPage(Item item)
{
InitializeComponent();
BindingContext = this.ioModModels = new IoModulesViewModels(item);
Label header = new Label
{
Text = "ListView",
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
ListView lw = new ListView
{
ItemsSource = ioModModels.modlists,
HasUnevenRows = true,
ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
Label modHeaderName = new Label();
modHeaderName.SetBinding(Label.TextProperty, "Name");
Label modHeaderType = new Label();
modHeaderType.SetBinding(Label.TextProperty, "Type");
Label modHeaderVersion = new Label();
modHeaderVersion.SetBinding(Label.TextProperty, "Version");
Label modHeaderSerial = new Label();
modHeaderSerial.SetBinding(Label.TextProperty, "Serial");
Label modHeaderStatus = new Label();
modHeaderStatus.SetBinding(Label.TextProperty, "Status");
grid.Children.Add(modHeaderName);
grid.Children.Add(modHeaderType, 1, 0);
grid.Children.Add(modHeaderVersion, 2, 0);
grid.Children.Add(modHeaderSerial, 3, 0);
grid.Children.Add(modHeaderStatus, 4, 0);
ListView lp = new ListView
{
ItemsSource = ioModModels.modlists,
HasUnevenRows = true,
ItemTemplate = new DataTemplate(() =>
{
Label header2 = new Label
{
Text = "ID,Type,GasComp,MeasurementType,Value",
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
HorizontalOptions = LayoutOptions.Fill
};
Grid gridIn = new Grid();
Label modID = new Label();
modID.SetBinding(Label.TextProperty, "getIDs");
Label modIOType = new Label();
modIOType.SetBinding(Label.TextProperty, "list.IOType");
Label modGasComp = new Label();
modGasComp.SetBinding(Label.TextProperty, "GasComp");
Label modMeasurementType = new Label();
modMeasurementType.SetBinding(Label.TextProperty, "list[0].MeasurementType");
Label modValue = new Label();
modValue.SetBinding(Label.TextProperty, "list[0].Value");
gridIn.Children.Add(modID);
gridIn.Children.Add(modIOType, 1, 0);
gridIn.Children.Add(modGasComp, 2, 0);
gridIn.Children.Add(modMeasurementType, 3, 0);
gridIn.Children.Add(modValue, 4, 0);
gridIn.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
gridIn.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
return new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 10),
Orientation = StackOrientation.Vertical,
Children =
{
header2,
gridIn
}
}
};
})
};
Frame frame = new Frame()
{
BorderColor=Color.Gray,
CornerRadius = 5,
Padding = 8,
Content = new StackLayout
{
Orientation=StackOrientation.Horizontal,
Children =
{
grid,
lp
}
}
};
return new ViewCell
{
View = new StackLayout
{
Padding = new Thickness(0, 10),
Spacing = 2,
Orientation = StackOrientation.Horizontal,
Children =
{
frame
}
}
};
}
)
};
// Accomodate iPhone status bar.
this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
// Build the page.
this.Content = new StackLayout
{
Children =
{
header,
lw
}
};
}
}
Как вы можете видеть в файле xaml, я пробовал множество аппроксимаций, в которых я могу связать список IOSensors, но пока что не повезло. Страница загружается и показывает некоторые результаты (например, синтаксис с «list [0] .property» работает, но я хочу, чтобы он работал для каждой записи списка.
Спасибо in dvance