C#

Prism for WinRT Applications–Sketchy Explorations

Following up on my previous post about Prism for WinRT applications, I thought I’d start digging in and trying to see what’s present in the Prism framework libraries. I wasn’t entirely sure how to approach this. With the Prism bits you get three things; The...(read more)

Create Your Own Android Toolkit In C#

c#tool

You may remember that a while back, we brought you news of a guide for creating your own Android utilities for Windows. Although fully functional and incredibly simple to put together, command line utilities can often feel a little rough around the edges. If you have your own custom tool but would like to make it look a little more polished, this might be of interest to you.

XDA Forum Member QuantumCipher has put together a guide for creating similar tools for Windows using C#, which provides the opportunity for a much cleaner looking interface. The guide covers creating a utility capable of performing basic tasks such as ADB commands to push/pull files, reboot your device, and install APK files. Once you know how to do this, it’s possible to create tools for much more complex tasks such as rooting and unlocking devices. If you have some basic knowledge of C# already, you’ll have no trouble diving straight into this tutorial. However, complete beginners might want to do a little research on the basics before getting started.

The guide fully explains the code required to add ADB functions to the elements of the interface, as well as how to use a text box to select a file to be pushed to the device. It should have you well on the way to creating your own toolkit in no time. Check out the tutorial thread for more information.


Taking Baby Steps with ScriptCS

I’ve been following the ScriptCS project with great interest over the last couple of months. As you may know by now, I’ve been searching for a more lightweight .NET development experience for quite some time. The ScriptCS project is here to fulfill this desperate need. Driven by the open-source .NET community, this wonderful initiative promises to unleash C# from Visual Studio which is exactly what I’ve been looking for.

Being heavily inspired by the node.js development workflow, ScriptCS is built on top of Roslyn and makes heavy use of NuGet for installing packages and script packs. You can get up and running in no time by installing ScriptCS using Chocolatey. If you didn’t have Chocolatey installed already (like me), then it can be easily done by running the following Powershell command:

@powershell -NoProfile -ExecutionPolicy Unrestricted -Command "iex ((New-Object Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1′))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin 

Now we can install ScriptCS by simply running the following command:

cinst scriptcs

That’s all we need!

Now in order to do something useful with it, we can clone the samples repository to our dev machine and try to run the Nancy sample application. Navigate to the Nancy folder in a command prompt and run the command “scriptcs -install” to install all package dependencies defined in the packages.config file. Next run the following command to start the web application:

image

Now open up your favorite browser, navigate to http://localhost:1234 and et voilà:

image

Amazing isn’t it?

I do recognize that the project is still very much a work in progress. Also something that’s not entirely clear for me is the deployment story. The road map mentions some kind of export functionality to an .exe or a Visual Studio Project. In any case, there should be an easy way to convert the code files into binary form.

Something that I also would love to see is support for Mono. How cool would it be to develop and run C# code using ScriptCS on Mac and Linux?

The future for ScriptCS certainly looks bright and I’m very much looking forward to where this particular road is taking the .NET universe. Take a look at the wiki and start familiarize yourself with ScriptCS as it’s going to open up very interesting opportunities for future .NET development.

Until next time.


Wherefore art thou, “Composite Application Guidance” ?

Ok, the title is meant to be a little jokey and is with reference to both Shakespeare and to The Gaslight Anthem and that’s quite a mix to start a blog post with Quite a while ago, I made a set of videos on the PRISM framework which was also known by the earlier...(read more)

Objective-C Associated Object

As an alternative to subclassing, Objective-C provides categories for extending core classes like NSString, NSMutableArray, etc. to add custom methods. In short, categories are nothing but a way to add extension methods similar to other languages (C#). One thing to keep in mind about categories is that it is not possible to synthesize properties or declare private variables inside a category. This is where the concept of associated object comes handy.

By definition, each object can have associated objects assessed by a key string. Baseline of Objective-C runtime is KVC or Key Value Coding pattern which is a mechanism for accessing an object’s properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables .

self.window = [someObject valueForKey:@"window"];

Each object also has an extended dictionary that let me store values in instance scope as associated objects.

Methods to set/get/remove associated objects are declared in obj/runtime.h header.

/* Associated Object support. */

/* objc_setAssociatedObject() options */
enum {
    OBJC_ASSOCIATION_ASSIGN = 0,
    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
    OBJC_ASSOCIATION_RETAIN = 01401,
    OBJC_ASSOCIATION_COPY = 01403
};
typedef uintptr_t objc_AssociationPolicy;

OBJC_EXPORT void objc_setAssociatedObject(id object
    , const void *key, id value
    , objc_AssociationPolicy policy
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
OBJC_EXPORT void objc_removeAssociatedObjects(id object)
    __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

Here AssociationPolicy enumeration let me specify the retention policy of an associated object inside memory similar to that of a property.

Although you can not synthesize a property inside category since categories can't hold the backing field that is generated in runtime for @synthesize but it is possible to declare dynamic properties using @dynamic keyword.This force me to write my own setter/getter methods where I can set/get value as associated objects.

Therefore, If I have a window property in category header like shown below:

@property (nonatomic, strong) UIWindow *window;

Inside the category implementation, I would first mark the property as dynamic:

@dynamic window;

Finally, I will write the getter/setter for the window property that will store value as associated object.

- (UIWindow*)window
{
    return objc_getAssociatedObject(self, @"window");
}

- (void)setWindow:(UIWindow *)window
{
    objc_setAssociatedObject(self, @"window", window, 
            OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

Inheriting class like NSMutableArray is tedious. Category is the most elegant option in extending a foundation class and associated object support makes it possible to store result objects dynamically inside a category.


  • Sponsored Links

  • June 2013
    M T W T F S S
    « May    
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
  • .

    Copyright © 1996-2010 Answer My Query. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress