Search code examples
c#linqmvvmwindows-store-apps

Converting WPF to Windows Store App Listbox issue


I've made a windows Desktop App and now in the process of making a Windows 8/8.1/10 Store App version of the program. I know I have to use more MVVM coding in order to get this to work appropriately, in addition to more code behind. For those that would like to see the original desktop program, it is located at https://github.com/JescoInc/Japanese-and-English-Speech-Dictionary

So, Here are the classes i've developed.

namespace WindowsEJDictionary
{
    public class xmlData
    {
        private string english;
        private string romaji;
        private string kanji;

        public string English
        {

            get { return english; }

            set { english = value; }

        }

        public string Romaji
        {
            get { return romaji; }

            set { romaji = value; }
        }

        public string Kanji
        {
            get { return kanji; }

            set { kanji = value; }
        }
    }
}

Main class:

    private void queryXML()
    {
        string xmlPath = "JapaneseEnglishData.xml";
        XDocument xDoc = XDocument.Load(xmlPath);

        var lstWord = xDoc.Root.Elements("Word").Select(x => new
        {
            English = x.Element("English").Value
        }).ToList();

        this.listBx.ItemsSource = lstWord;
        }
    }
   private void queryXMLTest2()
    {
        string xmlPath = "JapaneseEnglishData.xml";
        XDocument xDoc = XDocument.Load(xmlPath);

        var lstWord = from item in xDoc.Descendants("Word")
                      select new
                      {
                          English = item.Element("English").Value,
                      };
        foreach (var i in lstWord)
        {
            listBx.ItemsSource = i.English.ToList();
        }

XAML:

Alert me | Edit | Change type Question You cannot vote on your own post 0

I've made a windows Desktop App and now in the process of making a Windows 8/8.1/10 Store App version of the program. I know I have to use more MVVM coding in order to get this to work appropriately, in addition to more code behind. For those that would like to see the original desktop program, it is located at https://github.com/JescoInc/Japanese-and-English-Speech-Dictionary

So, Here are the classes i've developed.

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        queryXML();
    }

    private void queryXML()
    {
        string xmlPath = "JapaneseEnglishData.xml";
        XDocument xDoc = XDocument.Load(xmlPath);

        var lstWord = xDoc.Root.Elements("Word").Select(x => new
        {
            English = x.Element("English").Value
        }).ToList();

        this.listBx.ItemsSource = lstWord;
        }
    }
}

XAML code:

<Page
    x:Class="WindowsEJDictionary.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsEJDictionary"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto">
            </RowDefinition>
        </Grid.RowDefinitions>

        <Button Content="Button" HorizontalAlignment="Left" Margin="718,625,0,0" VerticalAlignment="Top" Width="144"/>
        <ListBox Name="listBx" HorizontalAlignment="Left" Height="398" Margin="109,255,0,0" VerticalAlignment="Top" Width="494"/>

    </Grid>
</Page>

and XML File:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--This is a generated XML File-->
<WordList>
  <Word>
    <English>Sunday</English>
    <Romaji>nichiyoubi</Romaji>
    <Kanji>にちようび</Kanji>
  </Word>
  <Word>
    <English>Weekend</English>
    <Romaji>shuumatsu</Romaji>
    <Kanji>しゅうまつ</Kanji>
  </Word>
  <Word>
    <English>vacation</English>
    <Romaji>yasumi</Romaji>
    <Kanji>やすみ</Kanji>
  </Word>
  <Word>
    <English>rest</English>
    <Romaji>yasumi</Romaji>
    <Kanji>やすみ</Kanji>
  </Word>
  <Word>
    <English>Goodnight</English>
    <Romaji>oyasumi nasai</Romaji>
    <Kanji>おやすみなさい</Kanji>
  </Word>
</WordList>

Okay, now some background. Test 1 and Test 2 produce different results but both produce results that are unwanted. So, Here are the links to the screenshots of the results. (Note: I am LINQ challenged, I suck royally at it, which is the main reason for my issue).

queryXMLMethod queryXMLText2Method


Solution

  • So, the answer to my question is as followed. rather than using LINQ and Lambda expressions, I should only need to use LINQ by itself. So, lets break this down.

    var listOEnglish = from wordList in XDoc.Root.Elements("Word")

    // This line is declaring the Root Element that we want to iterate through.

    select wordList.Element("English").Value;

    // this line is setting the child element we want to iterate through with the words within the tag of English.

    listBx.ItemsSource = listOEnglish.ToList();

    // this line sets the itemsource or the databinding for the list box to be from the listOEnglish as a list.

    Many Thanks to Jesse from the FlatRedBall (FRB) community for showing me how to just work with LINQ and help me come up with the solution.

    private void queryXML2()
    {
        string xmlPath = "JapaneseEnglishData.xml";
        XDocument XDoc = XDocument.Load(xmlPath);
    
        var listOEnglish = from wordList in XDoc.Root.Elements("Word")
                           select wordList.Element("English").Value;
        listBx.ItemsSource = listOEnglish.ToList();
    }