Archive for the ‘.NET’ Category

I’ve constructed a primitive batch based on the Microsoft and Farseer examples that can apply a texture to a 2D polygon.

Texture configuration properties are stored in the ‘user data’ property in the Farseer Body and Fixture objects.

I use the body position to determine the texture coordinates. This allows you to apply rotation and translation to the texture coordinates based on the body, in addition to the stored properties.

As you can see, the textures can be applied across multiple sub fixtures.

alphaEditor_03

XNA Development – Turnip Toss

Posted: December 27, 2012 in .NET, XNA
Tags: , ,

Working on a XNA game I am currently calling “Turnip Toss” using the Farseer physics engine.

The editor is in early alpha stages now.
2 dimensional “bodies” and simple shapes can be created by right clicking a position on the editing surface.

alphaEditor_01

alphaEditor_02

Converting a Frequency to a Midi Note

Posted: December 3, 2012 in .NET

This is a C# method to convert a pitch or frequency to a midi note.
I am shifting detected frequencies to the nearest semi-tone before using this formula.


private double GetMidiNote(float freq)
{
return Math.Ceiling(69 + 12 * (Math.Log(freq/440, 2)));
}

Introduction to Umbraco

Posted: August 27, 2012 in .NET, Umbraco
Tags: ,

I recently hosted the first Fort Worth .NET User’s Group first SIG.

The topic was Umbraco CMS. Here is the deck –

 

(using MVC4 beta)

In some cases it is not easy to alter the header values to negotiate the content type returned from WEBAPI.

There is a QueryStringMapper object you can add to the Media Type Formatter objects that allow you to use a query string to negotiate the content format.

In the example below,  I am adding a query string parameter ‘format’ to the mapping. When ‘format’ == ‘xml’, the content type is ‘text/xml’ and the XmlMediaTypeFormatter is used.

I run this in the ‘Application_Start’ method of the Global.asax 

            foreach (var item in GlobalConfiguration.Configuration.Formatters)
            {
                if (typeof(XmlMediaTypeFormatter) == item.GetType())
                {
                    item.AddQueryStringMapping("format", "xml", "text/xml");
                }
            }

Example api call – http://localhost/api/foo?format=xml

Here is an example of aggregation using LINQ to a collection –

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GroupByExampleLinq
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string AccountNumber { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Example();
            Console.ReadKey();
        }

        static void Example()
        {
            // Creating new List of Person
            List personCollection = new List()
            {
                //Adding a new Person
                new Person(){
                    AccountNumber = "123123 634566",
                    Email = "qwerty@qwerty.com",
                    FirstName = "John",
                    LastName = "Doe"
                },
                //Adding a new Person
                new Person(){
                    AccountNumber = "123123 678879",
                    Email = "qwerty@qwerty.com",
                    FirstName = "John",
                    LastName = "Doe"
                },
                //Adding a new Person
                new Person(){
                    AccountNumber = "123123 122134",
                    Email = "qwerty@qwerty.com",
                    FirstName = "John",
                    LastName = "Doe"
                },
                //Adding a new Person
                new Person(){
                    AccountNumber = "123123 890980",
                    Email = "asdf@asdf.com",
                    FirstName = "Jane",
                    LastName = "Doe"
                }
            };

            // Using LINQ to query the collection and group them by the email address
            var personQuery = from p in personCollection
                              group p by p.Email into g
                              select new { Email = g.Key, Accounts = g };

            // Output the email address and the count of records for each group
            foreach (var item in personQuery)
            {
                Console.WriteLine("{0} has {1} accounts.", item.Email, item.Accounts.Count());
            }
        }
    }
}

Posted: March 9, 2012 in .NET, SharePoint

Thank you W0ut. This saved me some time.

I have recently seen an issue that popped up in a couple different projects I have worked on.

When using a wizard inside Visual Studio, the available datasets list has been empty. I was able to solve this once after exhausting other build/rebuild techniques by creating a new project, then copying all the source files from the orginal project.

I’m really not sure where the orginal error was in the project, but the same thing happened today in a different project. Rather than creating a new project, I read in this post you could create an ‘index.aspx’ and recompile – it worked!

I’m not sure if this is a bug, but if you get the scenario where your business objects are not available as options in a VS wizard, try adding an index.aspx and let me know if it works…

I’m using the first Umbraco 5 (Jupiter) RTM on a new project.

One of the requirements of this project is to aggregate RSS feeds, so I built a simple plugin leveraging the System.Xml.Linq objects.

To make the plugin, I created a new MVC 3 project in visual studio to build and test the functionality.
I would have added the Umbraco test site to my solution and setup post build commands to copy the files from my project to Umbraco folders, but for some reason I was not able to run the Umbraco site from Visual Studio. I think its a system problem though.

I’ve also used the HTML Agility pack to parse the feed descriptions and remove bad markup.

Here is the  code to grab the RSS Feed and parse it with the HTMLAgilityPack –


    public static class Reader
    {
         public static IEnumerable GetRssFeed(string feedUrl, bool trimDescription = true)
         {
             XDocument feedXml = XDocument.Load(feedUrl);
             XNamespace dc = "http://purl.org/dc/elements/1.1/";
             string exp = @"^.{1,500}";
             if (trimDescription == false)
                 exp = ".*";
             var feeds = from feed in feedXml.Descendants("item")
                         select new Rss
                         {
                             Title = feed.Element("title").Value,
                             Link = feed.Element("link").Value,
                             Description = Regex.Match(ParsedDoc(feed.Element("description").Value).DocumentNode.InnerText, exp).Value,
                             PubDate = feed.Element("pubDate").Value,
                             Creator = feed.Element(dc + "creator").Value
                         };
             return feeds;
         }

         public static HtmlAgilityPack.HtmlDocument ParsedDoc(string source)
         {
             HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
             doc.OptionWriteEmptyNodes = true;
             doc.OptionFixNestedTags = true;
             doc.LoadHtml(source);
             return doc;
         }


    }

 

And the RSS class…

 


    public class Rss : IComparable
    {
        public string Link { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime PubDateDate { get; set; }
        public string Creator { get; set; }


        public string PubDate 
        {
            get
            {
                return _pubDate;
            }
            set
            {
                _pubDate = value;
                DateTime pubDateDate = DateTime.MinValue;
                DateTime.TryParse(value, out pubDateDate);
                PubDateDate = pubDateDate;
            }
        }
        private string _pubDate;

        // Implement the default CompareTo method with the PubDate
        // as the parameter. 
        //
        public int CompareTo(Rss other)
        {
            // If other is not a valid object reference, this instance is greater.
            if (other == null) return 1;
            return PubDateDate.CompareTo(other.PubDateDate);
        }


    }

In Umbraco, I created a folder in the plugins directory following the Dev Dataset examples.
I placed the HTMLAgilityPack DLL and XML, and my DLL in this folder to allow the CSHTML to call the classes.

I created 2 new document types in Umbraco for an ‘RSS List’ and ‘RSS Feed Item’. On the RSS List template, I grab all the children of that node (which are of type ‘RSS Feed Item’) and grab the properties from them using the DynamicModel.

In the Umbraco RSS List template…


@inherits Umbraco.Cms.Web.RenderViewPage

@functions {
    //Add public properties here to create Macro Parameters

    protected List<Zeroth.Umbraco.RSSReader.Rss> aggregatedFeed = new List<Zeroth.Umbraco.RSSReader.Rss>();
}

@{
    foreach (var child in DynamicModel.Children)
    {
        aggregatedFeed.AddRange(Zeroth.Umbraco.RSSReader.Reader.GetRssFeed(child.url, true));
    }
    aggregatedFeed.Sort();
    aggregatedFeed.Reverse();
}

<h2>@DynamicModel.CurrentPage.Name</h2>

<div class="rssList">
@foreach (var child in aggregatedFeed)
{
    <div class="rssItem">
        <div class="rssLink">
            <a href="@child.Link" target="_blank">@Html.Encode(child.Title)</a>
        </div>
        <div class="rssByLine">
            <span class="rssPubDate">@child.PubDate.Replace("+0000", "")</span><span> | </span><span class="rssCreator">@child.Creator</span>
        </div>
        <div class="rssDescription">
            @( new HtmlString(child.Description) )
        </div>
    </div>
}
</div>

I just picked up a GHI Panda II for some development with the .NET Micro Framework. Very excited about tinkering in the embedded world.