Building a Silverlight DataTemplate in c# code - c#

This might be more of an outcry, but building DataTemplates in Silverlight code seems to be impossible. The one option I am aware of being using XamlReader.Load(string), one is met with several issues using this approach:
No validation, everything is done in concatenated strings. We all know this sucks
Any necessary resources have to be included, as this will run in its own context. This also sucks, although there is a logical explanation and not a show-stopper.
Any attempts to include event calls inside your DataTemplate will fail with InvalidOperationException: Invalid XAML for control: XamlReader.Load() does not accept event handlers.
No.3 seems to be a show-stopper for me. I would appreciate it if anyone gave some clues, pointers or ideas towards another / better way of doing this.
In WPF there is a FrameworkElementFactory that can provide assistance, however there seems to be no remedy for this in Silverlight.

Maybe you dont need to build datatemplates in code. Do you really need to do that?
(#2) You dont need to include the xaml resource. it could reside remotely somewhere. all you have to do is load it remotely. But maybe im not understanding what you said.

Related

Move all events out of the designer

I have a form with many controls, each having events (and their handlers), total some tens events .
I found out that many times, due to complex combinations, events are being fired while the controls are being initiated, mainly because I load saved settings from my settings file that may change the default controls' initial settings, causing events to fire.
To avoid this, I moved all my events to a special method (in Main) that is being called only after all controls have been built and and set.
It works fine, but the question is if this is good or common practice and what drawbacks it may have.
I have also tried to move the events to a special Maim subclass, but could not find a way to get access to the private controls from the subclass.
Not sure what you are expecting as an answer here. If the code works well enough, and is reasonably clear, I can't really see the problem.
Furthermore, it sounds as if this is necessary in your case: If you need to set up or modify controls based on saved state from settings, then it seems clear that this needs to be done before you can add events for those controls. As I already mentioned: As long as your code works fine, and is clear and easy to understand, it should be OK (the latter point matters because it means you will be able to fix it without too much hassle if it should prove to be problematic for some unforeseen reason later).
PS: If you want some more feedback about this, then you should add some actual code to comment on. That however, would probably imply that this question is better suited for the CodeReview StackExchange site than for this site. Perhaps you should add code, and post the question there instead.

Trying to build a Query Builder Control in WPF

Please bear with me for the length of this question, I tried to be as descriptive as possible. I am new to WPF programming. I am basically trying to build the RadDataFilter control:
RadDataFilter is used to filter collection by building query expression. The control is generally used by bringing all data from the database and then filtering on them.
However, I am trying to use it to build SQL query expression and then I query the database to retrieve the data. Our company has license for Telerik products so I had to try and use the control. Unfortunately, the control is not flexible to the extent that I would want to customize the Left Dropdown (RadComboBox).
As we have several items which users can filter on, it is not feasible to provide them with a single dropdpwn. I would like to have a custom control instead of the dropdown.
I have the following workaround to this problem:
Extract the Template for that control and override it in my XAML as described here. However, inspite of doing this, I am unable to change the control. I assume it is due to my inadequate knowledge in WPF.
Try to change the Source code for Telerik (We have the license for the code too). However with this approach, we need to keep making changes with every upgrade, which is high maintenance for the team.
Try to build a custom control.
I am looking for the 3rd approach. If there is an easy way (using the first 2 approaches) or any other solution to achieve this, please let me know and I shall try that.
To implement the 3rd approach, I am thinking to use a TreeView with each node using a ControlTemplate (containing my Custom Control). I will override the ToString() method of the Custom Control that gives me the one query condition. Once done, I would iterate through the children of the root node to build the final query expression.
However as I notice, we need 2 different ControlTemplates (one which allows creating a filter expression and the other with the operator, similar to RadDataFilter). I do not know how to set multiple templates like this.
This may sound simple to some of the developers out there, but I am really new to C# and WPF programming and would need time and experience to be comfortable with it.
Please let me know if you know any other solution that I can try to solve this problem. If you feel it is better to create a custom control, please guide me on whats the best way to implement this. Also comment on my approach to create the custom control.
Thanks!
I resolved it. Took the first approach:
"Extract the Template for that control and override it in my XAML as described here.."

ASP.NET MVC 3 Razor View Restrictions

I apologize in advance for the generic nature of my question, but I was unable to find any helpful advice from people trying to do the same thing as me on the web. Let me describe my scenario:
I am providing end users/designers of a website the ability to customize their views by storing the views (using Razor) in the database. I have all of this working, but my question is the following; From a security standpoint, how can I ensure and enforce that unwanted code doesn't get executed in the user-defined view? There are two basic approaches that I think will work conceptually, but am not sure which one is more possible or feasible.
Option 1: Create a validation method in the administration tool that allows the user to input the view code. This would need to either take a whitelist or blacklist approach to what is allowable or not.
Option 2: Prevent unwanted code from being able to execute when rendering of the view occurs.
As a quick example of something that would need to be blocked, we wouldn't want to allow access to read or write files, access any data access functions, or even access configuration settings, etc. in the web.config. There will likely be a decently-sized list of things that probably shouldn't be allowable, but I'll need to sit down and try to think of as many security-related concerns as possible.
My question then is, which method would be the best bet? Also, can any direction be provided on how to go about either? I thought I might be able to make trust-level based change which would be Option 2, but couldn't find any way to make that work in a per-view based manor (the administration code is allowed to execute whatever it wants). I'm thinking Option 1 will end up being the best bet and I'll have to check for the input of certain framework functions that shouldn't be allowed. Does anyone have any experience doing anything like what I'm trying to do? ANY feedback is much appreciated!
This would be extremely difficult.
You could run the the template through the Razor preprocessor, then use Roslyn (still in early beta) to parse the generated file and look through all method calls (or constructors) and return an error if it calls something you don't like.
I strongly recommend that you use a whitelist for that, since the .Net framework is big enough that you are bound to overlook something in a blacklist.
However, I would instead recommend that you not use Razor at all and instead use a templating engine that does not allow real C# code.

How does a WPF application know where to start?

I'm BRAND NEW to the whole C#/WPF thing. I have a decent understanding of the concept of the WPF layering and it is a very nice tool. What I am running into, however, is that VS and the like try to make things very hands-off as far as the underlying code.
When firing up a brand new WPF application in VS C# Express 2008, there are two immediately visible source files: App.xaml and Window1.xaml. This is all fine and dandy, but the only place I see any significance of where things start is the line in App.xaml that says
<Application x:Class="SomeName.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
Looking into the class name of SomeName.App, I'm guessing that extending Application signifies that as where to start, but how does the application actually know that?
I am quite familiar with Java, so if it makes things easier to explain that way, please do. I like to understand things at the lowest level possible (without getting into machine code), so please help me get a bit deeper into the inner workings of C# and the WPF.
As always, thanks to the StackOverflow community for any help. :)
The concept you probably need to understand is that the tool-chain generates code from XAML files, which gives 'code-like' behaviour to the declarative XAML.
But WPF is pretty complicated and not much like anything else, and a book might be useful - personally I think the Adam Nathan WPF book is excellent, and will cover this "general understanding of the concepts" stuff much better than the Internet, IMO.
The generated app file will probably be called app.g.cs, and will be in one of the intermediate file directories - have a look in there to see the actual startup code - among other things, you'll find something like:
public static void Main() {
MyAppName.App app = new MyAppName.App();
app.InitializeComponent();
app.Run();
}
at which point it may start to make more sense.
In fact, you can write all that startup code yourself if you don't like the declarative route.

Is there an existing way to turn source code back into a CodeCompileUnit?

We use the DesignSurface and all that good IDesignerHost goodness in our own designer. The designed forms are then persisted in our own bespoke format and all that works great. WE also want to export the forms to a text-based format (which we've done as it isn't that difficult).
However, we also want to import that text back into a document for the designer which involves getting the designer code back into a CodeCompileUnit. Unfortunately, the Parse method is not implemented (for, no doubt, good reasons). Is there an alternative? We don't want to use anything that wouldn't exist on a standard .NET installation (like .NET libraries installed with Visual Studio).
My current idea is to compile the imported text and then instantiate the form and copy its properties and controls over to the design surface object, and just capture the new CodeCompileUnit, but I was hoping there was a better way. Thanks.
UPDATE: I though some might be interested in our progress. So far, not so good. A brief overview of what I've discovered is that the Parse method was not implemented because it was deemed too difficult, open source parsers exist that do the work but they're not complete and therefore aren't guaranteed to work in all cases (NRefactory is one of those from the SharpDevelop project, I believe), and the copying of controls across from an instance to the designer isn't working as yet. I believe this is because although the controls are getting added to the form instance that the designer surface wraps, the designer surface is not aware of their inclusion. Our next attempt is to mimic cut/paste to see if that solves it. Obviously, this is a huge nasty workaround, but we need it working so we'll take the hit and keep an eye out for alternatives.
You could always write your own C# parser. That way you can be sure of it's completeness.
In your case, because you don't need anything like intellisense, you could probably get away with just using a parser generator.
Even if you wrote one by hand, however, it probably wouldn't take you more than about a month.
It's not exactly what you asked for, but you could try to use the CodeDomComponentSerializationService class to generate the CodeDom graph based on the current state of the design surface.
We use that class to handle copy/paste functionality in our built-in designer.

Categories