Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
RICHUNCLEPENNYBAGS
Dec 21, 2010
Well, OK, but you could just put a context with both things in a separate project and then it could be referenced by multiple things, too. There's nothing about Identity that ties it into one of those types of applications -- and if you don't care about users in one place or another you can just not look at that table.

Also, having two tables with the same information can cause problems with trying to keep the information in sync and you may later regret it. That's my personal experience anyway.

RICHUNCLEPENNYBAGS fucked around with this message at 02:26 on Mar 11, 2015

Adbot
ADBOT LOVES YOU

crashdome
Jun 28, 2011
It amuses me that to any layperson googling, .NET programming is centralized 100% around blog development and the occasional Customers/Orders/Products involved in Foo & Bar sales.

Cryolite
Oct 2, 2006
sodium aluminum fluoride
Seems about right.

Inverness
Feb 4, 2009

Fully configurable personal assistant.

NihilCredo posted:

If you must maintain XP compatibility and must therefore target .NET 4.0, is it a total no-brainer to grab the BCL Portability Pack to get access to async/await or are there any drawbacks to consider?
The only issue I see with it is that the target machine must have a certain .NET 4 update installed. The KB number is listed on the NuGet page.

Mr Shiny Pants
Nov 12, 2012
Quick WPF question:

If I have a view model that also holds my combobox items and I instantiate this in code:

code:
 this.datacontext = new ViewModel() 
How do reference this in my datatemplate?

Is it ok if I do:

code:
    
<StackPanel.DataContext>
	<local:ViewModel/>
</StackPanel.DataContext>
Does this retrieve the instance of the viewmodel I already instantiated in my code? Or does this instantiate a new viewmodel?

Edit: Bummer it seems to instantiate again. :( Now to find out how to get the DataContext already instantiated.....

Edit: This seems to work:

code:
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},Path=DataContext.Defects}"/>

Mr Shiny Pants fucked around with this message at 13:52 on Mar 11, 2015

Drastic Actions
Apr 7, 2009

FUCK YOU!
GET PUMPED!
Nap Ghost

Mr Shiny Pants posted:

Quick WPF question:

If I have a view model that also holds my combobox items and I instantiate this in code:

code:
 this.datacontext = new ViewModel() 
How do reference this in my datatemplate?

Is it ok if I do:

(Code above)


You set it in code, so you don't need to set it again. For example, this crappy app I just built.

code:
namespace TestWpfApp.ViewModel
{
    public class TestViewModel
    {
        public string TestString { get; set; } = "LOOK MAH! I'M SET IN CODE!";
    }
}
code:
<Window x:Class="TestWpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock 
            VerticalAlignment="Center" 
            HorizontalAlignment="Center" 
            FontSize="30" Text="{Binding TestString}"/>
    </Grid>
</Window>
[code/]

[code]
namespace TestWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new TestViewModel();
        }
    }
}


The Data Context is set on the window, so the textbox on the grid can be binded to it.

raminasi
Jan 25, 2005

a last drink with no ice

Drastic Actions posted:

You set it in code, so you don't need to set it again. For example, this crappy app I just built.

code:
namespace TestWpfApp.ViewModel
{
    public class TestViewModel
    {
        public string TestString { get; set; } = "LOOK MAH! I'M SET IN CODE!";
    }
}
code:
<Window x:Class="TestWpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock 
            VerticalAlignment="Center" 
            HorizontalAlignment="Center" 
            FontSize="30" Text="{Binding TestString}"/>
    </Grid>
</Window>

[code]
namespace TestWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new TestViewModel();
        }
    }
}
[/code]

The Data Context is set on the window, so the textbox on the grid can be binded to it.

If I'm understanding the question and remembering my WPF right, the question is about DataTemplates, which can get annoying to find DataContexts for. Unfortunately, I don't have anything in front of me I can refer to for anything concrete.

Drastic Actions
Apr 7, 2009

FUCK YOU!
GET PUMPED!
Nap Ghost

GrumpyDoctor posted:

If I'm understanding the question and remembering my WPF right, the question is about DataTemplates, which can get annoying to find DataContexts for. Unfortunately, I don't have anything in front of me I can refer to for anything concrete.

Yeah, you're right. I did not read the question correctly. It's early :(

Mr Shiny Pants
Nov 12, 2012

Drastic Actions posted:

Yeah, you're right. I did not read the question correctly. It's early :(

I edited my post. I think I found something that works. Thanks anyway. :)

Something else, this is probably dumb because it sounds pretty simple:

I have a list, you select something in the list and press a button to open the selected item.

How do I pass the selected item on the Buttonclick? Do I need to reference the listview?

My controls get binded dynamically so they have no name. Do I need to do button.parent.listview.selectedItem? Or is there something else?

Mr Shiny Pants fucked around with this message at 14:56 on Mar 11, 2015

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Mr Shiny Pants posted:

I edited my post. I think I found something that works. Thanks anyway. :)

Something else, this is probably dumb because it sounds pretty simple:

I have a list, you select something in the list and press a button to open the selected item.

How do I pass the selected item on the Buttonclick? Do I need to reference the listview?

My controls get binded dynamically so they have no name. Do I need to do button.parent.listview.selectedItem? Or is there something else?

The button should be bound to an ICommand to fire off a method in the viewmodel. You should not have an event handler.

Mr Shiny Pants
Nov 12, 2012

Ithaqua posted:

The button should be bound to an ICommand to fire off a method in the viewmodel. You should not have an event handler.

How would this work? The ListView in my GUI is bound to a Generic list that has no SelectedItem property? When I fire off the button pass in the Listview Selected Item?

How would my viewmodel know about a property of a xaml control?

Edit: Thinking about it some more:

I need a property in my viewmodel that gets updated when I select something right? That way I can get the property if the button is clicked from the datacontext.

Mr Shiny Pants fucked around with this message at 15:32 on Mar 11, 2015

raminasi
Jan 25, 2005

a last drink with no ice

Mr Shiny Pants posted:

Edit: Thinking about it some more:

I need a property in my viewmodel that gets updated when I select something right? That way I can get the property if the button is clicked from the datacontext.

Yep, bind ListView.SelectedItem or whatever to something in your viewmodel.

Mr Shiny Pants
Nov 12, 2012

GrumpyDoctor posted:

Yep, bind ListView.SelectedItem or whatever to something in your viewmodel.

Yeah that makes sense. Thanks for helping me in the right direction.

Mr Shiny Pants
Nov 12, 2012
I must say that I am pleasantly surprised by WPF.

I started this morning having never coded a line of WPF, but I am getting pretty good results already.

raminasi
Jan 25, 2005

a last drink with no ice

Mr Shiny Pants posted:

I must say that I am pleasantly surprised by WPF.

I started this morning having never coded a line of WPF, but I am getting pretty good results already.

WPF is known for making hard things easy and easy things hard. On the balance I think it's pretty great, but be prepared to eventually run into some absurd wart or bug down the line.

Mr Shiny Pants
Nov 12, 2012

GrumpyDoctor posted:

WPF is known for making hard things easy and easy things hard. On the balance I think it's pretty great, but be prepared to eventually run into some absurd wart or bug down the line.

One thing I don't get is why MS did not make this the default development environment for their tablets and phones. I am working on an application that can run on a tablet but for the really nice integration stuff ( like a camera :raise: ) I must program WinRT?

WTF?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Mr Shiny Pants posted:

One thing I don't get is why MS did not make this the default development environment for their tablets and phones. I am working on an application that can run on a tablet but for the really nice integration stuff ( like a camera :raise: ) I must program WinRT?

WTF?

That's a grand Microsoft tradition. Stick around and they'll probably come up with a whole 'nother island for watcheswearables or something.

ljw1004
Jan 18, 2005

rum

Mr Shiny Pants posted:

One thing I don't get is why MS did not make this the default development environment for their tablets and phones. I am working on an application that can run on a tablet but for the really nice integration stuff ( like a camera :raise: ) I must program WinRT?

WTF?

I'm sure you can access camera from WPF. I know that I wrote code which accessed it in a WinForms app, via underlying win32/COM APIs, so it must be possible from WPF.


As to why not WPF for tablets/phones? I'm not on the WPF or WinRT-XAML teams, so all this is just speculation, but...

* WPF isn't consumable by C++ or JS. Had to be redone from scratch to support those platforms.

* WPF is huge - lots of APIs and classes, fine for running on a desktop, but there's a lot more stuff happening in them than is worth spending battery on for a low power device. I believe the WinRT XAML stack can render stuff with many fewer CPU cycles.

* I'm not clear on this, but I get the impression that WPF falls too easily into "pits of failure" that can't be accelerated by a graphics card or offloaded onto a separate rendering thread. The WinRT XAML stack avoids those pits by default.

That said, there are lots of painful and irritating omissions in the WinRT XAML stack that I do miss from WPF.

Mr Shiny Pants
Nov 12, 2012
Oh I am sure I can get the camera to work, but I saw some RT demo's when it came out and it was like three lines of code.

All good points, but I think if they would have spent the energy on WPF that they did on RT I am sure a lot could be improved in the framework itself.

I mean they had Silverlight already, which is like WPF lite.

This looks promising: http://blogs.msdn.com/b/eternalcodi...-a-wpf-app.aspx

Mr Shiny Pants fucked around with this message at 19:46 on Mar 11, 2015

twodot
Aug 7, 2005

You are objectively correct that this person is dumb and has said dumb things
Not sure if anyone noticed, but one of the core GC documents is up in the Documentation folder on github:
https://github.com/dotnet/coreclr/blob/master/Documentation/garbage-collection.md

Adhemar
Jan 21, 2004

Kellner, da ist ein scheussliches Biest in meiner Suppe.

ljw1004 posted:

* I'm not clear on this, but I get the impression that WPF falls too easily into "pits of failure" that can't be accelerated by a graphics card or offloaded onto a separate rendering thread. The WinRT XAML stack avoids those pits by default.

This is really interesting, can you shed more light on how WinRT XAML avoids this? At my company we've used WPF for a while now, but we keep running into performance problems (mostly layout, we need lots of grids that constantly update). We've gone back to WinForms in some cases. I have no experience with WinRT stuff, could we benefit from WinRT XAML performance in a regular (non-Store) desktop app?

Mr Shiny Pants
Nov 12, 2012
Ok, roadblock.

Is ICommand complicated or what? I need a button that will add an item to a list, the item that has the list is itself in a list.

How do I do something like:

code:
() => this.List.Add(new Item());

Do I really need to implement relaycommands and the like? Or do I need to keep track of the item that has focus? If I click a button that is on a control in a list is SelectedItem filled?

I am tempted to just use on_button_click. :(

Mr Shiny Pants fucked around with this message at 21:44 on Mar 11, 2015

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!
I'm using DateTime to get the day of the month as a number, but I haven't been able to find a good way to format the day with suffixes. For instance, if the date is 5, I want to format the string to output "5th", 1 becomes "1st", etc.

Is this something custom I'll have to write?

Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!

enthe0s posted:

I'm using DateTime to get the day of the month as a number, but I haven't been able to find a good way to format the day with suffixes. For instance, if the date is 5, I want to format the string to output "5th", 1 becomes "1st", etc.

Is this something custom I'll have to write?

It's not built in because it would be impossible to properly globalize.

http://stackoverflow.com/questions/20156/is-there-an-easy-way-to-create-ordinals-in-c

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!
Gotcha, thanks!

gariig
Dec 31, 2004
Beaten into submission by my fiance
Pillbug

enthe0s posted:

I'm using DateTime to get the day of the month as a number, but I haven't been able to find a good way to format the day with suffixes. For instance, if the date is 5, I want to format the string to output "5th", 1 becomes "1st", etc.

Is this something custom I'll have to write?

Use Humanizr (pulled from The Hanselman)

EssOEss
Oct 23, 2006
128-bit approved

Mr Shiny Pants posted:

Ok, roadblock.

Is ICommand complicated or what? I need a button that will add an item to a list, the item that has the list is itself in a list.

How do I do something like:

code:
() => this.List.Add(new Item());

Do I really need to implement relaycommands and the like? Or do I need to keep track of the item that has focus? If I click a button that is on a control in a list is SelectedItem filled?

I am tempted to just use on_button_click. :(

The general pattern often used is MVVM (model-view-viewmodel) and if you want to do it "properly" you should likely follow that pattern. Roughly speaking, stop thinking of "handling" UI events. Instead bind the UI elements to various behaviors exposed by your viewmodel. What this means specifically depends very much on your actual business logic and is not realistically simplifyable.

For example, if you have an app to select a nice set of flowers from different options, you might have a FlowerSetBuilderVm viewmodel object for your FlowerSetBuilder form. The VM would expose an ObservableCollection<FlowerVm> named AvailableFlowers and another named SelectedFlowers. There would be a single Add button that enables flowers to be added from the available list to the selected list. This button is data bound to an AddCommand property of the selected FlowerVm (or none if no FlowerVm is selected). Pressing the button will execute the logic in the ICommand implementation exposed by the AddCommand property, which will execute code in the FlowerSetBuilderVm to move the relevant FlowerVm from one list to another. The UI will automatically detect when this happens and adjust itself accordingly, adding/removing view layer items based on the changes in the two collections.

Very generalized but I guess you get the point.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Mr Shiny Pants posted:

Is ICommand complicated or what? I need a button that will add an item to a list, the item that has the list is itself in a list.

I am tempted to just use on_button_click. :(

To expand on what EssOEss said, you need to think about the data that represents your UI without thinking in terms of buttons and labels and text fields. The beauty of MVVM is the same as in any other declarative UI framework, you describe how the UI should be rendered based off of your data, and then you can just work with your data without having to manually update the UI.

For example, what I gathered from your post was that you have a list of lists. For each inner list in your outer list, you want to be able to add an item to that inner list. Your data might look like this:

C# code:
public class OuterListVm
{
    public ObservableCollection<InnerListVm> InnerLists { get; set; }
}

public class InnerListVm
{
    public ObservableCollection<Item> Items { get; set; }
    public ICommand AddItemCommand
    {
        get { return new CommandHandler(AddItem); }
    }

    public void AddItem()
    {
        Items.Add(new Item());
    }
}
ICommand is a stupid simple interface that just says "do something with this object I give you". It's stupid simple so it can be used in a lot of situations. The most obvious one is binding it to a button's Click event. WPF handles the event subscription for you, all you have to do is tell it to bind the button's Command property to AddItemCommand in XAML.

The huge, huge (huge) benefit is that you can stop caring about the menial things like subscribing to events like button click, text changed, etc. and just work with the data that describes your view, i.e. your View Model. I guarantee you the vast majority of things you might do in a UI can be described simply with ICommand, string, int/double/decimal/etc. and ObservableCollection.



The CommandHandler in the above code is a very simple, copy-and-pastable implementation of ICommand:

C# code:
class CommandHandler : ICommand
{
    private readonly Action _action;
    private readonly bool _canExecute;

    public CommandHandler(Action action, bool canExecute = true)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute;
    }

    public void Execute(object parameter)
    {
        _action();
    }

    public event EventHandler CanExecuteChanged;
}

Bognar fucked around with this message at 22:39 on Mar 11, 2015

epswing
Nov 4, 2003

Soiled Meat

Mr Shiny Pants posted:

Ok, roadblock.

Is ICommand complicated or what? I need a button that will add an item to a list, the item that has the list is itself in a list.

How do I do something like:

code:
() => this.List.Add(new Item());

Do I really need to implement relaycommands and the like? Or do I need to keep track of the item that has focus? If I click a button that is on a control in a list is SelectedItem filled?

I am tempted to just use on_button_click. :(

Here's a quick VS 2012 example solution I whipped up, using ICommand and MVVM: http://epswing.com/shared/ICommandAndMVVM.zip

It's small enough that you should be able to read everything in a few minutes. Let me/us know if anything is unclear. Note that pretty much all the "logic" is in the ViewModel.

Mr Shiny Pants
Nov 12, 2012
Thanks guys, I will take a look tomorrow. I've been programming all day, I am spent.

Will let you know.

Just felt like I needed to build ISkyScraperFoundation when all I wanted was new Shed(); :)

Mr Shiny Pants fucked around with this message at 23:43 on Mar 11, 2015

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Hah just had a funny. I have a job fired by an .aspx page that connects to an FTP and uploads a file I've created. I was testing it out in another application so hurriedly just commented out the ftp code (because I didn't want to upload test files to the live server) and then let it run in the background to test the file generation part. Switched to something else for 30 minutes, look up and see that the browser tab I was running it from still had the spinning 'waiting for localhost' icon going. This thing should normally take 5 minutes top. Turns out I was a bit too lazy when commenting, and the guy who did this before me a bit too lazy when naming variables:
code:
' Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse) <-- this got commented out

' other stuff

response.Close() ' <-- This didn't get commented out
Turns out when you name a variable the same thing as a class bad things can happen.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Scaramouche posted:

Turns out when you name a variable the same thing as a class bad things can happen.

Maybe it's my lack of knowledge of VB here, but which variable is named the same as a class? I see the FtpWebResponse class and the 'response' variable name in that code.

ljw1004
Jan 18, 2005

rum

Adhemar posted:

This is really interesting, can you shed more light on how WinRT XAML avoids this? At my company we've used WPF for a while now, but we keep running into performance problems (mostly layout, we need lots of grids that constantly update). We've gone back to WinForms in some cases. I have no experience with WinRT stuff, could we benefit from WinRT XAML performance in a regular (non-Store) desktop app?

VS2015 will ship with "UI Performance Analysis Tool" for WPF desktop apps
http://blogs.msdn.com/b/wpf/archive/2015/01/16/new-ui-performance-analysis-tool-for-wpf-applications.aspx

It's already present in the latest CTPs of VS2015. Sounds like it will be directly relevant to your work.


Where does WinRT XAML avoid perf hits? I guess the first thing that comes to mind, and which bit me, is its thing about independent animations and EnableDependentAnimation being off by default. https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj819807.aspx

Adhemar
Jan 21, 2004

Kellner, da ist ein scheussliches Biest in meiner Suppe.

ljw1004 posted:

VS2015 will ship with "UI Performance Analysis Tool" for WPF desktop apps
http://blogs.msdn.com/b/wpf/archive/2015/01/16/new-ui-performance-analysis-tool-for-wpf-applications.aspx

It's already present in the latest CTPs of VS2015. Sounds like it will be directly relevant to your work.

Cool, thanks. I'll have to play around with that at home, it'll be a while before we're running VS 2015 and Win 8.1+ at work. :negative:

RICHUNCLEPENNYBAGS
Dec 21, 2010

Che Delilas posted:

Maybe it's my lack of knowledge of VB here, but which variable is named the same as a class? I see the FtpWebResponse class and the 'response' variable name in that code.

VB isn't case-sensitive and I'm guessing GetRequest returns a Request.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

RICHUNCLEPENNYBAGS posted:

VB isn't case-sensitive and I'm guessing GetRequest returns a Request.

Yeah. Right click on that 'Response.Close()' and choose to Go To Definition and you get the class in the Object Explorer, not a variable declaration.

brap
Aug 23, 2004

Grimey Drawer
God drat vb is messed up.

Sedro
Dec 31, 2008
Why would the Response class have a close method?

Mr Shiny Pants
Nov 12, 2012
It works! Yay!

epalm posted:

Here's a quick VS 2012 example solution I whipped up, using ICommand and MVVM: http://epswing.com/shared/ICommandAndMVVM.zip

It's small enough that you should be able to read everything in a few minutes. Let me/us know if anything is unclear. Note that pretty much all the "logic" is in the ViewModel.

Thanks for this! This made it a whole lot clearer. The relaycommand stuff I also saw while searching around. Is it a best practice to just do it that way? Your InotifyProperty changed stuff is pretty funky. Seems like a generic implementation?



Bognar posted:

To expand on what EssOEss said, you need to think about the data that represents your UI without thinking in terms of buttons and labels and text fields. The beauty of MVVM is the same as in any other declarative UI framework, you describe how the UI should be rendered based off of your data, and then you can just work with your data without having to manually update the UI.



Gotcha.

drat:

This got me good:

http://blog.alner.net/archive/2010/05/07/wpf-style-and-template-resources_order-matters.aspx

What started out as a pretty simple app......

You want a different type of template for a different Type? Use a datatemplate selector.

Oh you want it to change based on a property, datatemplateselector does not listen to PropertyChanged events.

So now I have datatemplate.triggers that watch a value.

It works, but it is something else.

It is cool though.

Mr Shiny Pants fucked around with this message at 10:57 on Mar 12, 2015

Adbot
ADBOT LOVES YOU

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Sedro posted:

Why would the Response class have a close method?

It doesn't, and I don't think that's what's going on here. ASP.NET has a property called Response on Web Forms pages and MVC controllers that accesses the HttpContext.Response object (which is an HttpResponse, I don't think just Response exists as a class).

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply