C
C#•3w ago
pauliology

Ping function message box not working

Hi all, I am trying to use several different buttons to ping different ip's. So far only the first 3 buttons have ip's on them using their tags There is no error output message and it builds correctly, however, the Ping messagebox is not displaying. Please see attached code here: https://paste.mod.gg/nnjlefslfqhc/1
BlazeBin - nnjlefslfqhc
A tool for sharing your source code with the world!
40 Replies
canton7
canton7•3w ago
I don't see any instances of PingButton_Click in your xaml, so how are your buttons wired to your codebehind?
pauliology
pauliologyOP•3w ago
Oh my apologies I changed all the names after pasting the code Still wasn't generating the popuo Popup
canton7
canton7•3w ago
Btw you can self-close tags if they have no content: <Button ..../> instead of <Button ...></Button> So, if the code you actually tried is different, update the code in your paste There aren't any click handlers in your XAML
pauliology
pauliologyOP•3w ago
@canton7 im looking at the latest bit of code you pasted in terms of how you would do it. I'm just wondering how would i apply the xaml. Does that take over my entire grid ?
canton7
canton7•3w ago
Yeah, the ItemsCollection dynamically generates buttons (using that DataTemplate), and lays them out using that WrapPanel That binding approach only really works if you're doing MVVM proper though -- it's painful if you're using the codebehind (but still do-able)
pauliology
pauliologyOP•3w ago
data template being this ? PingButtons = new BindableCollection<PingButton>()
canton7
canton7•3w ago
No, the <DataTemplate>
pauliology
pauliologyOP•3w ago
Yeah Would it be worth me converting my code to that now or not really ?
canton7
canton7•3w ago
Up to you
pauliology
pauliologyOP•3w ago
Umm well i have say 9 pages with about 5-10 of those buttons on each page is that easier ?
canton7
canton7•3w ago
Depends how much you like copy-pasting šŸ˜„
pauliology
pauliologyOP•3w ago
i don't mind if i knew exactly what i was doing šŸ˜„
canton7
canton7•3w ago
Time to learn!
pauliology
pauliologyOP•3w ago
@canton7 could I ask you for a massive flavour favour well massive for me, maybe miniscule for you I am struggling to understand how you converted the functions and xaml earlier. This is my current latest version. It also has a function of which a button will pull an image from an ip address. I've used google as an example. However thats wrong because the ip address will be the same ip address of the device above it. For example DE camera, and DE snapshot will be the sameip whats the chances of helping me fix my code to utiilse your way and fix that function at the same time. because i believe that will help with both ping and image function
pauliology
pauliologyOP•3w ago
BlazeBin - trllcnnxexbr
A tool for sharing your source code with the world!
pauliology
pauliologyOP•3w ago
@canton7 did that explanation even make any sense
canton7
canton7•3w ago
Sorry, haven't had a chance to look
pauliology
pauliologyOP•3w ago
all good, bud i know you are busy with your cuppa
pauliology
pauliologyOP•3w ago
No description
pauliology
pauliologyOP•3w ago
this is a visual of what im talking about so imagine the top row would be devices that i would use the ping function for so there could be 5-10 of them the bottom row i am hoping to achieve, will open an image of the camera using the same ip from the top device in a popup window that i can close
canton7
canton7•3w ago
So each "item" is two buttons, one on top of the other?
pauliology
pauliologyOP•3w ago
yes sir well under each other i have grid.row 0 = the ping function and grid.row 1 = the snapshot function
canton7
canton7•3w ago
I'd structure that as an ItemsCollection, with each item being that pair of buttons with some spacing between them It's annoyingly hard to data-bind to grids, useful as they are
pauliology
pauliologyOP•3w ago
Could you help me out with that? I've really got no clue
canton7
canton7•3w ago
I'm afraid I've got a chunk of work to do this afternoon
pauliology
pauliologyOP•3w ago
feel free to tell me to fuck off šŸ˜„ ok well in the interim, could you possibly just help correct my function slightly to save your time atm i have this
Image image = new Image();
var httpUrl = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png";
Image image = new Image();
var httpUrl = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png";
trying to get it to just take the ip from the Tag Property
canton7
canton7•3w ago
Trivially, you've got the sender right there
pauliology
pauliologyOP•3w ago
I've got this although the image didn't load Xaml
<CustomControls:FancyButton Image="/Images/cameraIcon.png" Title="DE Snapshot" Grid.Column="0" Grid.Row="1" x:Name="LoadImageButton" Click="LoadImageButton_Click" Tag="https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png"></CustomControls:FancyButton>
<CustomControls:FancyButton Image="/Images/cameraIcon.png" Title="DE Snapshot" Grid.Column="0" Grid.Row="1" x:Name="LoadImageButton" Click="LoadImageButton_Click" Tag="https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png"></CustomControls:FancyButton>
private async void LoadImageButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is string httpUrl)
{
try
{
var message = await http.GetAsync(httpUrl);
if (message.IsSuccessStatusCode)
{
var stream = await message.Content.ReadAsStreamAsync();
var image = new Image
{
Source = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
};

var imageWindow = new Window
{
Title = "Loaded Image",
Content = image,
Width = 800,
Height = 600
};

imageWindow.ShowDialog();
}
else
{
MessageBox.Show("Failed to load image.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Exception: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private async void LoadImageButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.Tag is string httpUrl)
{
try
{
var message = await http.GetAsync(httpUrl);
if (message.IsSuccessStatusCode)
{
var stream = await message.Content.ReadAsStreamAsync();
var image = new Image
{
Source = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad)
};

var imageWindow = new Window
{
Title = "Loaded Image",
Content = image,
Width = 800,
Height = 600
};

imageWindow.ShowDialog();
}
else
{
MessageBox.Show("Failed to load image.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Exception: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
no error message window pops up just blank
canton7
canton7•3w ago
What are you trying to do? Show a new window with the image in?
pauliology
pauliologyOP•3w ago
yes sir so the window popped up correctly the image didn't load in it
canton7
canton7•3w ago
Google suggests you need to set StreamSource? It'll be something to do with creating the ImageSource Look in the Output window for clues
pauliology
pauliologyOP•3w ago
ok last question for the night imagine this image
pauliology
pauliologyOP•3w ago
is kept behind a user/pass in the old days i could use http:// user:[email protected] to access image using my function i currently got, how do i implement that in c#
canton7
canton7•3w ago
That's a HttpClient thing Set the Authorization header to a new BasicAuthenticationHeaderValue or something IIRC? (assuming you're using basic authentication)
pauliology
pauliologyOP•3w ago
its weird i can get it to pull the google image but when i try this link it errors failed to load image
pauliology
pauliologyOP•3w ago
whats the reasoning behindthat function hasn't changed at all
canton7
canton7•3w ago
No idea. Use Fiddler to see what it's requesting, and a hint at why it's failing
pauliology
pauliologyOP•3w ago
will do its getting late for me, i have an early flight to the mines tomorrow 4am kinda why i was in a hurry to get it done, don't get to work on it while im away on the mines

Did you find this page helpful?