【发布时间】:2016-09-23 01:40:11
【问题描述】:
我有一个数据库,我可以从中获取我的“Baustellen”。现在我想通过 DataBinding 在 ListView 上显示他们的“strasse”,但他们没有显示出来。你知道为什么吗?
Baustelle.cs:
namespace SchlachterFliesen.Code
{
public class Baustelle
{
public int id;
public Kunde bauherr;
public Kunde auftraggeber;
public Ort ort;
public string strasse, hausnr, bauauftrag, status;
public Baustelle()
{
}
public Baustelle(int id, Kunde bauherr, Kunde auftraggeber, Ort ort, string strasse, string hausnr, string bauauftrag, string status)
{
this.id = id;
this.bauherr = bauherr;
this.auftraggeber = auftraggeber;
this.ort = ort;
this.strasse = strasse;
this.hausnr = hausnr;
this.bauauftrag = bauauftrag;
this.status = status;
}
public override string ToString()
{
return
"Baustelle@" + GetHashCode() + "\r\n" +
"ID: " + id + "\r\n" +
"Bauherr: " + bauherr?.name + "\r\n" +
"Auftraggeber: " + auftraggeber?.name + "\r\n" +
"Ort: " + ort?.name + "\r\n" +
"Straße: " + strasse + "\r\n" +
"Hausnummer: " + hausnr + "\r\n" +
"Bauauftrag: " + bauauftrag + "\r\n" +
"Status: " + status;
}
}
}
Baustelle.xaml.cs
namespace SchlachterFliesen
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet oder zu der innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class Baustelle : Page
{
private List<Code.Baustelle> Baustellen;
public Code.Baustelle[] baustellen;
public Baustelle()
{
this.InitializeComponent();
Daten.LadeAlleBaustellen(new Daten.Ziel<Code.Baustelle[]>(empfaenger)); //HERE I GRAB THEM!
}
public void empfaenger(Code.Baustelle[] baustellen)
{
this.baustellen = baustellen;
Baustellen = GetBaustellen();
}
public List<Code.Baustelle> GetBaustellen()
{
var baustellenListe = new List<Code.Baustelle>();
if (baustellen.Length > 0)
{
for (int i = 0; i < baustellen.Length; i++)
{
baustellenListe.Add(new Code.Baustelle(baustellen[i].id, baustellen[i].bauherr, baustellen[i].auftraggeber, baustellen[i].ort, baustellen[i].strasse, baustellen[i].hausnr, baustellen[i].bauauftrag, baustellen[i].status));
}
}
//myTextBlock.Text = res;
return baustellenListe;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
currentView.BackRequested += backButton_Tapped;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
var currentView = SystemNavigationManager.GetForCurrentView();
currentView.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;
currentView.BackRequested -= backButton_Tapped;
}
private void backButton_Tapped(object sender, BackRequestedEventArgs e)
{
var rootFrame = Window.Current.Content as Frame;
var mainPage = rootFrame.Content as MainPage;
if (rootFrame != null && rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.Navigate(typeof(MainPage));
rootFrame.GoBack();
}
}
}
}
Baustelle.xaml
<Page
x:Class="SchlachterFliesen.Baustelle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SchlachterFliesen"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data ="using:SchlachterFliesen.Code"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Frame Name="baustelleFrame">
<Grid>
<ListView Name="baustellenListView" ItemsSource="{x:Bind Baustellen}" HorizontalAlignment="Center">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Baustelle">
<StackPanel>
<TextBlock FontSize="10" Text="{x:Bind strasse}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Frame>
</Grid>
</Page>
【问题讨论】:
-
在这里查看我的最小示例:stackoverflow.com/questions/25613212/… 它适用于 WUA8.1,但它仍然适用于您的 W10 项目。
-
Daten.LadeAlleBaustellen和Daten.Ziel<Code.Baustelle[]>是什么?由于您的代码不完整,我无法重现您的问题。你能分享minimal reproducible example吗?另外,你有没有试过像Baustellen = new List<Baustelle> { new Baustelle { strasse = "1" }, new Baustelle { strasse = "2" }, new Baustelle { strasse = "3" } };这样在构造函数中设置Baustellen,这样行吗?
标签: c# xaml win-universal-app windows-10 uwp