Is it really important to use pack URIs in WPF apps? - wpf

Why should we use those, rather than ordinary ones?
what's the benefits of using this:
new Uri("pack://application:,,,/File.xaml");
over that:
new Uri("/File.xaml", UriKind.Relative);

The first one - you can use cross-assembly by adding a assembly-name after the three commas. So, you can create a shared library with common styles and other XAML-goodness that can be shared between several assemblies.
Syntax is like this:
pack://application:,,,/Common;component/CommonResources.xaml
where Common is the name of the assembly and everything after component is the path inside that assembly to the mapped resource. The latter can only be used inside the same assembly (and should be preferred).
I use it a lot for ResourceDictionaries residing in a common assembly above several module-type assemblies.

Related

Is it possible to override the .mustache-files that are in the Gollum-gem?

I'm asking myself if I can change the .mustache templates to use my own html in my Gollum-wiki. If so it doesn't seem to documented (or am I just blind?).
I mean this: https://github.com/gollum/gollum/blob/master/lib/gollum/templates/layout.mustache
Can I override it or is the only way to have a different markup to use a different wiki software?
It is possible, you can simply:
Make your changes to the templates you need and put them on a directory of your choice
Also copy any other (probably unmodified) templates that Gollum requires (this is a bit clunky, see Gollum's issue #1221) to that directory
Specify the template-dir option when launching Gollum
That will make Gollum to search for templates only in that location. At the time of writing, there is no fallback template location, or a way to override single templates. In any case, just copying all templates to template-dir will do exactly what you need.

Global variables across modules

OK, so this is the concept :
I'm currently writing a fairly complex project, consisting of 10's of different modules and classes.
I need to have one basic set of variables/options (an associative array?) which will be shared (read/write) by all modules (or selected ones) at any time.
What would be the most D-friendly way to achieve this?
UPDATE:
Hmm... just created a variable definition in one module (let's say globals.d module) and no matter where I import it, I can always get/set it. That simple?! (Or am I missing anything?)
Just filling this in so there's an answer: yes, you generally would want to make a new module like globals.d and simply import it from all the other modules that use it.

WPF - Is <?Mapping > used anymore?

Basically, I remember that there was a time when you could use the following: <?Mapping ... > (It doesn't seem to work anymore -- Deprecated perhaps?)
I know I can map my XML namespaces like so...
xmlns:xyzcon="clr-namespace:XYZ.Wpf.Controls"
xmlns:xyzcom="clr-namespace:XYZ.Wpf.Commands"
But, I also know that I can map my XML namespaces like so (much cleaner)
[assembly: XmlnsPrefix("http://schemas.mycompany.com/netfx/xaml/presentation", "xyz")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Controls")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Commands")]
which makes it very easy to use because instead having the l, lv, etc. prefix I can have all my CLR-Namespaces point to the same XML namespace...
xmlns:xyz="http://schemas.mycompany.com/netfx/xaml/presentation"
That line gives me access to XYZ.Wpf.Controls and XYZ.Wpf.Commands. The problem is that this only works if the assembly is already compiled. I can't use this trick when working within the same assembly.
Basically, from within the same app or assembly, I want to map different CLR namespaces to the SAME XML namespace on a global scale (that way throughout my app I simply have to include that one XML namespace). Any way to do that?
It appears that there is still no way to map local namespaces within a WPF project. The book Programming WPF: Building Windows UI with Windows Presentation Foundation (published in 2007) states:
WPF projects in Visual Studio cannot use namespaces introduced by XmlnsDefinitionAttribute from XAML that live in the same assembly. If you want to refer to locally defined types, you must use the alternative mechanism...
The "alternative mechanism" being the dreaded xmlns:local="..." for each namespace.
My understanding is that the executing assembly (in this case the local WPF assembly) searches all referenced assemblies for both XmlnsPrefixAttribute and XmlnsDefinitionAttribute but does not search itself. It makes no sense that 4 years later this behavior has not changed, but that appears to be the case.

A Guide for Creating your own Library for Cocoa(touch) development

I'm currently using a lot of the same subclassed objects with custom methods. It would be more convenient to create my own library which I can use for several projects.
The goal is to have my own classes being available in the same way classes like UIView, CGRect etc are, including convenient methods like CGRectMake, both with classes and structs. To sum it up, I want to create my own equivalents of:
Classes like UIView
Structs like CGRect
Convenient functions like CGRectMake
Have this available as a library
Have this available as an XCode template, thus, having these custom Objects available as 'new files' in XCode
So basically I'm looking for instructions on how to create classes, structs etc in order to create all the above. What is the best way to do this? The 320 project seems like a good starting point. But it lacks (I think) in:
having the library available in new projects right away
having the new classes available under 'new file'
Even if I would create an own static library, will I be able to release the app on the app store, since linking to 3rd party libraries is not supported on the phone?
For your convenience, these are basically the sub questions, covering the scope of this question:
How can I create my own library for Mac / iPhone development?
How do I create classes, structs and inline function for this library?
How do I create my own Xcode template based on this library?
Will I be able to release iPhone apps using my own static library?
FYI Xcode 3.2 has a new project template called Cocoa Touch Static Library. You might want to go that route.
If you were doing this for a Mac, you'd create a framework. However, you mention UIView, so obviously you're working with the iPhone. Apple doesn't allow iPhone applications to dynamically link against other libraries at runtime, so your only option is to create a static library. A static library is linked into the application executable when it's built.
To my knowledge, there's no static library project template in Xcode. What you'll likely have to do is start with a different iPhone Xcode template and add a Static Library target. Hang on to the default application target; you can use that to build a simple test application to make sure the library actually works.
To actually use the library in an application, you'll need two things: the compiled library (it has a .a extension) and all the header files. In your finished application, you'll link against your static library, and you'll need to #import the header files so that the compiler understands what classes, functions, etc. are available to it. (A common technique is to create one header file that imports all the others. That way, you only need to import a single file in your source files.)
As for creating your own custom templates, there's a simple tutorial here that should get you started: http://www.macresearch.org/custom_xcode_templates You can probably copy the default templates and just customize them to suit your purposes.
The struct syntax looks like this:
typedef struct _MyPoint {
CGFloat x;
CGFloat y;
} MyPoint;
Structs are are declared in header files, so you can (I believe) Command+Double Click on the name of a struct to see how it's declared.
Another little trick for creating structs is to do something like this:
MyPoint aPoint = (MyPoint){ 1.5f, 0.25f };
Because the compiler knows the order of fields in the struct, it can very easily match up the values you provide in the curly braces to the appropriate fields. Of course, it's more convenient to have a function like MyPointMake, so you can write that like this:
MyPoint MyPointMake(CGFloat x, CGFloat y)
return (MyPoint){ x, y };
}
Note that this is a function, not a method, so it lives outside of any #interface or #implementation context. Although I don't think the compiler complains if you define it in an #implementation context.
CGPointMake is defined as what's known as an inline function. You can see the definition for that in the Cocoa header files, too. (The difference between an inline function and a normal function is that the compiler can replace a call to CGPointMake with a copy of CGPointMake, which avoids the overhead of making a function call. It's a pretty minor optimization, but for a function that simple, it makes sense.)
The 320 project is a good example of an iPhone class library. You basically compile your project down into a .a library and then statically link against this in your client projects.
Since this is a community wiki now, I thought it will be helpful to link some resources and tutorials:
http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html
http://www.clintharris.net/2009/iphone-app-shared-libraries/
Enjoy!
The 320 project seems like a good starting point indeed. But it lacks (I think) in:
having the library available in new projects right away
having the new classes available under 'new file'
Those are project and file templates. For more information, ask the Google.
If you plan on releasing this on the app store, you wont be able to use your library in the way that you would like. As mentioned above, linking to 3rd party libraries is not supported on the phone. I think there is a 'hack' way to make it work, but you'll lose distribution.
The best I could come up with was putting all the relevant code in a directory and sharing it that way. I know its not as elegant, but its their limitation ie. out of our control.

Is it a bad practice to have multiple classes in the same file?

I used to have one class for one file. For example car.cs has the class car. But as I program more classes, I would like to add them to the same file. For example car.cs has the class car and the door class, etc.
My question is good for Java, C#, PHP or any other programming language. Should I try not having multiple classes in the same file or is it ok?
I think you should try to keep your code to 1 class per file.
I suggest this because it will be easier to find your class later. Also, it will work better with your source control system (if a file changes, then you know that a particular class has changed).
The only time I think it's correct to use more than one class per file is when you are using internal classes... but internal classes are inside another class, and thus can be left inside the same file. The inner classes roles are strongly related to the outer classes, so placing them in the same file is fine.
In Java, one public class per file is the way the language works. A group of Java files can be collected into a package.
In Python, however, files are "modules", and typically have a number of closely related classes. A Python package is a directory, just like a Java package.
This gives Python an extra level of grouping between class and package.
There is no one right answer that is language-agnostic. It varies with the language.
One class per file is a good rule, but it's appropriate to make some exceptions. For instance, if I'm working in a project where most classes have associated collection types, often I'll keep the class and its collection in the same file, e.g.:
public class Customer { /* whatever */ }
public class CustomerCollection : List<Customer> { /* whatever */ }
The best rule of thumb is to keep one class per file except when that starts to make things harder rather than easier. Since Visual Studio's Find in Files is so effective, you probably won't have to spend much time looking through the file structure anyway.
No I don't think it's an entirely bad practice. What I mean by that is in general it's best to have a separate file per class, but there are definitely good exception cases where it's better to have a bunch of classes in one file. A good example of this is a group of Exception classes, if you have a few dozen of these for a given group does it really make sense to have separate a separate file for each two liner class? I would argue not. In this case having a group of exceptions in one class is much less cumbersome and simple IMHO.
I've found that whenever I try to combine multiple types into a single file, I always end going back and separating them simply because it makes them easier to find. Whenever I combine, there is always ultimately a moment where I'm trying to figure out wtf I defined type x.
So now, my personal rule is that each individual type (except maybe for child classes, by which a mean a class inside a class, not an inherited class) gets its own file.
Since your IDE Provides you with a "Navigate to" functionality and you have some control over namespacing within your classes then the below benefits of having multiple classes within the same file are quite worth it for me.
Parent - Child Classes
In many cases i find it quite helpful to have Inherited classes within their Base Class file.
It's quite easy then to see which properties and methods your child class inherits and the file provides a faster overview of the overall functionality.
Public: Small - Helper - DTO Classes
When you need several plain and small classes for a specific functionality i find it quite redundant to have a file with all the references and includes for just a 4-8 Liner class.....
Code navigation is also easier just scrolling over one file instead of switching between 10 files...Its also easier to refactor when you have to edit just one reference instead of 10.....
Overall breaking the Iron rule of 1 class per file provides some extra freedom to organize your code.
What happens then, really depends on your IDE, Language,Team Communication and Organizing Skills.
But if you want that freedom why sacrifice it for an iron rule?
The rule I always go by is to have one main class in a file with the same name. I may or may not include helper classes in that file depending on how tightly they're coupled with the file's main class. Are the support classes standalone, or are they useful on their own? For example, if a method in a class needs a special comparison for sorting some objects, it doesn't bother me a bit to bundle the comparison functor class into the same file as the method that uses it. I wouldn't expect to use it elsewhere and it doesn't make sense for it to be on its own.
If you are working on a team, keeping classes in separate files make it easier to control the source and reduces chances of conflicts (multiple developers changing the same file at the same time). I think it makes it easier to find the code you are looking for as well.
It can be bad from the perspective of future development and maintainability. It is much easier to remember where the Car class is if you have a Car.cs class. Where would you look for the Widget class if Widget.cs does not exist? Is it a car widget? Is it an engine widget? Oh maybe it's a bagel widget.
The only time I consider file locations is when I have to create new classes. Otherwise I never navigate by file structure. I Use "go to class" or "go to definition".
I know this is somewhat of a training issue; freeing yourself from the physical file structure of projects requires practice. It's very rewarding though ;)
If it feels good to put them in the same file, be my guest. Cant do that with public classes in java though ;)
You should refrain from doing so, unless you have a good reason.
One file with several small related classes can be more readable than several files.
For example, when using 'case classes', to simulate union types, there is a strong relationship between each class.
Using the same file for multiple classes has the advantage of grouping them together visually for the reader.
In your case, a car and a door do not seem related at all, and finding the door class in the car.cs file would be unexpected, so don't.
As a rule of thumb, one class/one file is the way to go. I often keep several interface definitions in one file, though. Several classes in one file? Only if they are very closely related somehow, and very small (< 5 methods and members)
As is true so much of the time in programming, it depends greatly on the situation.
For instance, what is the cohesiveness of the classes in question? Are they tightly coupled? Are they completely orthogonal? Are they related in functionality?
It would not be out of line for a web framework to supply a general purpose widgets.whatever file containing BaseWidget, TextWidget, CharWidget, etc.
A user of the framework would not be out of line in defining a more_widgets file to contain the additional widgets they derive from the framework widgets for their specific domain space.
When the classes are orthogonal, and have nothing to do with each other, the grouping into a single file would indeed be artificial. Assume an application to manage a robotic factory that builds cars. A file called parts containing CarParts and RobotParts would be senseless... there is not likely to be much of a relation between the ordering of spare parts for maintenance and the parts that the factory manufactures. Such a joining would add no information or knowledge about the system you are designing.
Perhaps the best rule of thumb is don't constrain your choices by a rule of thumb. Rules of thumb are created for a first cut analysis, or to constrain the choices of those who are not capable of making good choices. I think most programmers would like to believe they are capable of making good decisions.
The Smalltalk answer is: you should not have files (for programming). They make versioning and navigation painful.
One class per file is simpler to maintain and much more clear for anyone else looking at your code. It is also mandatory, or very restricted in some languages.
In Java for instance, you cannot create multiple top level classes per file, they have to be in separate files where the classname and filename are the same.
(C#) Another exception (to one file per class) I'm thinking of is having List in the same file as MyClass. Where I envisage using this is in reporting. Having an extra file just for the List seems a bit excessive.

Resources