TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Alex Biddle
NA
10
5.3k
Windows 8.1 blank app data bind to next page
Aug 16 2016 2:50 PM
I am creating a Windows 8.1 blank application in C#. The purpose of the application is to be a periodic table app which will be able to give users information about the chemical elements. However I have come into an issue where I haven't yet found a way to 'bind' data to a second page via a button click.<div></div><div>Below is the code I have been using to set the values on the MainPage.xaml.cs page to populate the TextBlocks on the 'ElementPage.xaml' depending on the button user clicks</div><div></div><div> <div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Elements </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">string</span><span> PeriodicElementName { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></span></li><li><span> <span class="keyword">public</span><span> </span><span class="keyword">string</span><span> ElementAtomicNumber { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></span></li><li class="alt"><span> </span></li><li><span> <span class="keyword">public</span><span> </span><span class="keyword">string</span><span> ElementAtomicMass { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></span></li><li class="alt"><span> </span></li><li><span> <span class="keyword">public</span><span> </span><span class="keyword">string</span><span> ElementDescription { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> Elements ShowElements(</span><span class="keyword">string</span><span> Name, </span><span class="keyword">string</span><span> AtomNumber, </span><span class="keyword">string</span><span> AtomMass, </span><span class="keyword">string</span><span> ElementDesc) </span></span></li><li><span> { </span></li><li class="alt"><span> var ElementsInfo = <span class="keyword">new</span><span> Elements() </span></span></li><li><span> { </span></li><li class="alt"><span> PeriodicElementName = <span class="string">"Name: "</span><span> + Name, </span></span></li><li><span> ElementAtomicMass = <span class="string">"Atomic Mass: "</span><span> + AtomMass, </span></span></li><li class="alt"><span> ElementAtomicNumber = <span class="string">"Atomic Number: "</span><span> + AtomNumber, </span></span></li><li><span> ElementDescription = <span class="string">"Description: "</span><span> + ElementDesc </span></span></li><li class="alt"><span> }; </span></li><li><span> <span class="keyword">return</span><span> ElementsInfo; </span></span></li><li class="alt"><span> } </span></li></ol></div></div><div></div><div>I.e. whenever the user clicks on the Lithium button they will be taken to a page which will then show information based off of the data I have set in the class in the behind code file.</div><div></div><div> </div><div></div><div> <br />I followed the answer described in the question http://stackoverflow.com/questions/24946575/passing-data-from-page-to-page-for-windows-phone-8-1</div><div></div><div>However, it did not work for me.</div><div></div><div>I have tried one method which did work which involved creating a public string variable named 'ElementChoice' in my 'MainPage.xaml.cs' file and then changing the variable value to the name of the element whenever that specific element's button was clicked.</div><div></div><div> <div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">string</span><span> ElementChoice; </span></span></li></ol></div></div><div></div><div><br /> <div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">private</span><span> </span><span class="keyword">void</span><span> btnLith_Click(</span><span class="keyword">object</span><span> sender, RoutedEventArgs e) </span></span></li><li><span> { </span></li><li class="alt"><span> ElementChoice = <span class="string">"Lithium"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span> </span></li><li><span> </span></li><li class="alt"><span> } </span></li></ol></div></div><div></div><div> </div><div></div><div>I would then have an if statement on the page I am navigating to named 'ElementPage.xaml.cs' saying </div><div></div><div> </div><div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">if</span><span> (ElementChoice==</span><span class="string">"Lithium"</span><span>) </span></span></li><li><span> { </span></li><li class="alt"><span> DataContext = Elements.ShowElements(<span class="string">"Lithium"</span><span>, </span><span class="string">"3"</span><span>, </span><span class="string">"6.94"</span><span>, </span><span class="string">"This is Lithium"</span><span>); </span></span></li><li><span> } </span></li></ol></div><div></div><div><br />However, I don't want to really have to create if statements for every single button.</div><div></div><div>So I was wondering if anybody could give me any guidance on how to pass the data from one page to another which could then bind the data depending on what button was clicked on the application?</div>
Reply
Answers (
0
)
I have created Blog in my project(MVC4 with C#).But its load
annot implicitly convert type 'System.Xml.XmlNode'