Click here to Skip to main content
15,888,113 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: JavaScript-driven UWP via VStudio Pin
raddevus14-Aug-18 15:24
mvaraddevus14-Aug-18 15:24 
GeneralRe: JavaScript-driven UWP via VStudio Pin
Super Lloyd14-Aug-18 12:26
Super Lloyd14-Aug-18 12:26 
GeneralRe: JavaScript-driven UWP via VStudio Pin
raddevus14-Aug-18 15:27
mvaraddevus14-Aug-18 15:27 
GeneralRe: JavaScript-driven UWP via VStudio Pin
Shao Voon Wong17-Aug-18 1:05
mvaShao Voon Wong17-Aug-18 1:05 
JokeRe: JavaScript-driven UWP via VStudio Pin
Richard Deeming17-Aug-18 7:08
mveRichard Deeming17-Aug-18 7:08 
GeneralRe: JavaScript-driven UWP via VStudio Pin
Shao Voon Wong17-Aug-18 23:24
mvaShao Voon Wong17-Aug-18 23:24 
GeneralRe: JavaScript-driven UWP via VStudio Pin
Super Lloyd18-Aug-18 8:19
Super Lloyd18-Aug-18 8:19 
GeneralRe: JavaScript-driven UWP via VStudio Pin
Shao Voon Wong21-Aug-18 4:13
mvaShao Voon Wong21-Aug-18 4:13 
Let me illustrate with the below code. mSource used to be local variable but it is disposed before it can be used in the 4th lambda. So I make this temporary variable as a class member to work around that problem.
C++
void ClampImageApp::MainPage::btnOpenImage_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    FileOpenPicker^ fileOpenPicker = ref new FileOpenPicker();
    fileOpenPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
    fileOpenPicker->FileTypeFilter->Append(L".jpg");
    fileOpenPicker->ViewMode = PickerViewMode::Thumbnail;

    mSource = ref new SoftwareBitmapSource();
    concurrency::create_task(fileOpenPicker->PickSingleFileAsync())
    .then([&](StorageFile^ file)->IAsyncOperation<IRandomAccessStream^>^
    {
        if (file)
        {
            return file->OpenAsync(FileAccessMode::Read);
        }
        else
        {
            throw task_canceled();
        }
    }).then([&](IRandomAccessStream^ stream)->IAsyncOperation<BitmapDecoder^>^
    {
        // Create the decoder from the stream
        return BitmapDecoder::CreateAsync(stream);

    }).then([&](BitmapDecoder^ decoder)->IAsyncOperation<SoftwareBitmap^>^
    {
        return decoder->GetSoftwareBitmapAsync();

    }).then([&](SoftwareBitmap^ sw_bmp)->IAsyncAction^
    {
        if (sw_bmp->BitmapPixelFormat != BitmapPixelFormat::Bgra8 ||
            sw_bmp->BitmapAlphaMode == BitmapAlphaMode::Straight)
        {
            sw_bmp = SoftwareBitmap::Convert(sw_bmp, BitmapPixelFormat::Bgra8, BitmapAlphaMode::Premultiplied);
        }
        return mSource->SetBitmapAsync(sw_bmp);

    }).then([&]()->void {
        this->imgPhoto->Source = mSource;

    });
}

This is the C# code to use composite value
C#
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

// Create a composite setting
Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1212;
composite["strVal"] = "string";

localSettings.Values["RawCompositeSetting"] = composite;

// Read data from a composite setting
Windows.Storage.ApplicationDataCompositeValue composite2 =
   (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["RawCompositeSetting"];

And compared to C++/CX equivalent code
C++
Windows::Storage::ApplicationDataContainer^ localSettings =
    Windows::Storage::ApplicationData::Current->LocalSettings;

// Create a composite setting
ApplicationDataCompositeValue^ composite = ref new ApplicationDataCompositeValue();
composite->Insert("intVal", dynamic_cast<PropertyValue^>(PropertyValue::CreateInt32(1212)));
composite->Insert("strVal", dynamic_cast<PropertyValue^>(PropertyValue::CreateString("string")));

auto values = localSettings->Values;
values->Insert("RawCompositeSetting", composite);

// Read data from a composite setting
ApplicationDataCompositeValue^ composite2 = safe_cast<ApplicationDataCompositeValue^>(localSettings->Values->Lookup("RawCompositeSetting"));

UWP is a native platform where native C++/CX is orders of magnitude slower than managed C#. Not sure how that is possible. Microsoft just developed a new C++ language to replace C++/CX, called C++/winrt which has async/await and much better performance and full C++ standard compliant. But it is not ready yet as 2 months ago, I found VS cannot build a newly generated C++/winrt project without errors and it does not support win2D.
GeneralRe: JavaScript-driven UWP via VStudio Pin
Sander Rossel19-Aug-18 0:00
professionalSander Rossel19-Aug-18 0:00 
GeneralRe: JavaScript-driven UWP via VStudio Pin
raddevus19-Aug-18 7:03
mvaraddevus19-Aug-18 7:03 
GeneralRe: JavaScript-driven UWP via VStudio Pin
Ryan Peden27-Aug-18 4:01
professionalRyan Peden27-Aug-18 4:01 
GeneralRe: JavaScript-driven UWP via VStudio Pin
raddevus29-Aug-18 13:31
mvaraddevus29-Aug-18 13:31 
GeneralRe: JavaScript-driven UWP via VStudio Pin
wout de zeeuw28-Aug-18 4:41
wout de zeeuw28-Aug-18 4:41 
GeneralRe: JavaScript-driven UWP via VStudio Pin
raddevus29-Aug-18 13:36
mvaraddevus29-Aug-18 13:36 
GeneralRe: JavaScript-driven UWP via VStudio Pin
BillWoodruff2-Sep-18 20:14
professionalBillWoodruff2-Sep-18 20:14 
Generalenable developer mode in Visual Studio Pin
Clifford Nelson12-Sep-18 5:21
Clifford Nelson12-Sep-18 5:21 
GeneralRe: enable developer mode in Visual Studio Pin
raddevus12-Sep-18 5:24
mvaraddevus12-Sep-18 5:24 
GeneralWhat Version Is My C# Compiler? Pin
David A. Gray13-Aug-18 15:42
David A. Gray13-Aug-18 15:42 
GeneralRe: What Version Is My C# Compiler? Pin
Richard Deeming14-Aug-18 0:42
mveRichard Deeming14-Aug-18 0:42 
GeneralRe: What Version Is My C# Compiler? Pin
David A. Gray14-Aug-18 7:19
David A. Gray14-Aug-18 7:19 
GeneralRe: What Version Is My C# Compiler? Pin
Nish Nishant21-Aug-18 9:57
sitebuilderNish Nishant21-Aug-18 9:57 
GeneralRe: What Version Is My C# Compiler? Pin
Nelek21-Aug-18 10:05
protectorNelek21-Aug-18 10:05 
GeneralRe: What Version Is My C# Compiler? Pin
David A. Gray21-Aug-18 10:34
David A. Gray21-Aug-18 10:34 
GeneralRe: What Version Is My C# Compiler? Pin
Richard Deeming21-Aug-18 10:40
mveRichard Deeming21-Aug-18 10:40 
GeneralRe: What Version Is My C# Compiler? Pin
Nish Nishant21-Aug-18 10:47
sitebuilderNish Nishant21-Aug-18 10:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.