WPF Binding and Using Custom Sample Data in Visual Designer - c#

Quick 1 sentence summary: I wrote a demo app [download src here][1] that doesn't properly display sample data in the Visual Studio Designer and I need help.
After 6+ years developing in C# and WinForms, I've decided to use WPF in my current project. I've written a small demo application to teach myself and experiment with WPF (you can see a screenshot [here][1]). The app simply downloads and displays recent news stories from the Google News RSS feed.
My first attempt at this resulted in the class called "GoogleNewsWidget." After reading about the MVVM model, however, I tried again and built "GoogleNewsWidget2" that attempts to utilize a more MVVM-oriented architecture. I'm not sure which implementation is best as they both seem to be working fine on the whole (and though I'd appreciate comments on which is better, it is not my primary question).
My main problem is that neither play very well with the Visual Studio Designer. The GoogleNewsWidget2 loads and displays data fine when its xaml is opened directly but does not display correctly when embedded into another xaml file . The GoogleNewsWidget does not display data in the Designer in either case.
Any help would be appreciated. Again, the source is available for download [here][1].
Thanks,
Jon
[1]: http://abstract.cs.washington.edu/~jfroehli/reflect/ Demo App Source Code
PS My original post had multiple hyperlinks to screenshots but its posting was denied by StackOverflow for spam prevention reasons. Thus, I created the [1] url, which contains screenshots and a link to source code. If someone could also help me figure out how to use the "Markdown" language for linking, I'd be grateful. :)

Assuming that the VS2010 designer works the way Blend 3 does, you have to provide "dummy" data -- the designer won't pull data from external sources.
Create an object that implements the same interface as your datasource (view model) and fill it with static data. Make sure it has a public, no-arg constructor.
Define it as a resource in your control, giving it a key like "DesignData".
In the root element of your control, add the attribute d:DataContext={DynamicResource DesignData}"
This will be used as the DataContext only when in the designer.

Related

Pragmatically Open Database connection

I am working on a project and have scoured the internet for answers on this topic but could not find a single answer, not even an open source solution.
Maybe you guys could help me out.
In visual studio you can open the database connection window by browsing View -> server explorer -> add connection. that is all well and known but what if I wanted to open that same form from code or a certain API. I cant seem to figure it out but there must be some sort of way to access this form pragmatically since its native to Visual Studio.
For all who are unsure what form I'm talking about, here's a snippet:
So my question to you is, how can I , through code, open this window? If there is nothing built into Visual Studio and C#, do you know of any open source software that accomplishes this or will I have to recreate it with a windows form application?
I think you would need to build the form yourself if you need any custom logic etc in which case you could use the ConnectionStringBuilder classes and pass the validated fields from your custom form to the relevant builder class to generate the connection string. Visit here for more information.
Microsoft also has library on nuget that you could use, although it's deprecated so use with caution. The nuget file, which you can download manually, also contains the source code so perhaps you could use this to jump start your own UI component. https://www.nuget.org/packages/DataConnectionDialog

how to add two xaml page with same name and separate code file behind in uwp

Hi I want to have two different layouts, one for mobile other for pc and for this I want to add two xaml page with same name MainPage.xaml and code file behind MainPage.xaml.cs. One page added into DeviceFamily-Mobile folder and other in main area of project.
Now when I run the app it gives this error:
"Type 'MainPage' already defines a member called '.ctor' with the same parameter types"
To overcome this problem I add parameter in constructor of mainpage for desktop. Now project runs successfully and both page and code works good but I just noticed that parameterized constructor of desktop page not being called...
Now my summarized question is how can i add two page with same name and different code file behind for specific device family.
I have search a lot but did not get answer of my question.
Please suggest me any solution of my problem any tip.
Thanks
Note: One more thing, I don't want different xaml page with same one code file.
i) To create a page which targets different devices the XAML file is named using a specific syntax based on MRT. We have tooling support for this in Visual Studio:
First, create a new folder in your project by right clicking on the project->Add New Item->New Folder. This folder needs to be named to target a particular device family – in this case we will use Mobile (which includes Phone) using ‘DeviceFamily-Mobile’.The tailored XAML view is then created in Visual Studio using the ‘XAML view’ template. Select the folder you added, then right click->Add New Item->XAML View. Make sure you edit the name to be MainPage.xaml (by default it will be named MainPage1.xaml). You need to use the same name so that the code behind will be shared an the page correctly loaded.
Both views share the same code behind (MainPage.xaml.cs in the above example), and creating an event handler in one can also be used in the other view. When the app is run, Universal Windows will pick the correct view based on the device family that the app is being run on.
ii) If you want to special case code for a particular view you can test for the devicefamily as follows
if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile")
You can not. You must create 2 difference Page to have difference code behind.
I would suggest:
Refactor your code behind using dependency injection and interface so it adaptive to device family
The Pages in UWP are partial witch means you can create a new cs and just write public partial class MainPage{} and continue the class from there. You could then use conditional compilation.
As I searched a bit I couldn't find any conditional compiling in UWP but only for Universal(Second link). But the first link might help.
Three ways to set specific DeviceFamily XAML Views in UWP
Conditional Compilation in Universal Apps

XAML markup extensions design time

https://xamlmarkupextensions.codeplex.com/
I'm having a problem using the xamlmarkupextensions library. This library provides the opportunity to Bind values from a resource file (resx) to different controls. Now this works perfectly, but I don't want to use the resource file... I would like to use my own database.
So I made a new database and tried some stuff and got it working. The only problem is that the value is not visible in design time... Does anybody here knows how this could work? Thanks in advance!

Displaying images or controls in Visual Studio source view

I'm working on a number-crunching app with some fairly (from my POV) complex math. My first thought was,
Gee, it'd be really nice to be able to take a screen shot in
Mathematica and include it as a comment in my C# source.
I see a cdata value when entering /// alongside summary, remarks, and other options. Custom tags appear to also be supported (as there doesn't appear to be any validation happening).
In order of preference, I'd like to:
Link Mathematica code to my C# source, and have said code appear, in all its Greek-laden glory, as a comment above the method that calls it (or from which it was generated). Bonus points for actually embedding the Mathematica editor in VS!
Link to an image file containing a screenshot of the above, manually generated via Paint and the snip tool, and have VS display it.
Embed the image in a CDATA section, and have it automagically render inside of VS when viewing the code, inline with said code. Generating a base-85 (or whatever) encoded version of an image is trivial. The problem at this point is getting it to display.
Ways that this is possible:
Write an app that overlays an image on the screen and, via magic, keeps it aligned with the source view in VS. (This is ridiculous, but, hey, so is a language that requires you to denote variable names with a $, and yet has a full BNF grammar available to anyone who can Google. Oh, what a world...)
I got nothing.
Suggestions? Is it possible to extend VS so that an image, or control (where Mathematica could be hosted), can appear in the source view?
(note: one of my favorite statements is "It's software. Anything is possible. Give me a 9-volt battery, a paper clip, pocket knife and a monkey hopped up on speed, and we'll make it happen." In this case, I mean within the realm of practicality.)
It looks like this Visual Studio plug-in should solve most of your requirements:
http://visualstudiogallery.msdn.microsoft.com/793d16d0-235a-439a-91df-4ce7c721df12

Application Links item in Open/SaveFileDialog in C#

When showing the open file dialog, I want to show up the "App,lication Links" category in the left side pane on Vista/7. Visual Studio does that too when opening project files.
How can I do this with a standard OpenFileDialog-Object in C#? I am using WPF by the way, but I don't think that matters in this case.
You need to use FileDialog.CustomPlaces. That MSDN link contains some sample code to get you going. Ultimately this is wrapping the native functionality exposed by IFileDialog::AddPlace.

Categories