xamarin.forms find out what picture is set to imagesource - xamarin.forms

I have a picture and I need to find out what picture is set to it:
I tried:
if (img_adPic1.Source == ImageSource.FromFile("img_camera"))
{...}
But that never returns true, even when it states on the imagesource, that under file it is listed as "img_camera".
How do I find out what picture is set to imagesource?
Than you!

You could use the FileImageSource to compare.
var s = img_adPic1.Source as FileImageSource;
if (s.File == "pink.jpg")//filename with extension
{
}

Related

How to write if statement comparing numbers that creates objects conditionally in QML

I'm working with QML and Python3.6 + PySide2 and I'm trying to write script in QML that takes two integers from a connection in python and compares them to decide what image background to use for the window.
There are a few things I'm struggling with. First, I am unsure how to compare my numeric (sunset and sunrise) variables. Second, I don't know how to write an if statement-esque part that produces a background image conditionally. Third, I don't think it's best to do this under Connections, and maybe even in my QML, but I'm not sure how to move my variables somewhere else.
I really appreciate any pointers or help!!
The data I'm drawing from looks like this:
"sunrise":1592565499,"sunset":1592617094
The QML pseudo-ish code:
Connections {
target: weather
function onDataChanged(){
if(!weather.hasError()){
var sunrise = weather.data['dt']['sunrise']
var sunset = weather.data['dt']['sunset']
if (sunrise <= sunset)
Image {
source: "night.png"}
else
Image {
course: "day.png"}
}
You cannot create QML-items from if-statements like that (neither from State's). You can call Qt.createComponent if you like, however, it is rather overkill in this example. You should directly set the source property of your image:
Image {
id: image_tod
}
Connections {
target: weather
function onDataChanged(){
if(!weather.hasError()){
var sunrise = weather.data['dt']['sunrise']
var sunset = weather.data['dt']['sunset']
if (sunrise <= sunset)
image_tod.source = "night.png"
else
image_tod.source = "day.png"
}
}
}
Looking at the code, you might actually be able to bind it directly to the source property (not sure what your model exactly looks like):
Image {
source: {
if(weather.data['dt']['sunrise'] <= weather.data['dt']['sunset'])
return "night.png"
else
return "day.png"
}
}
This works because when compiling the QML, the engine creates a dependency from every referenced variable (weather and data in this case), and re-evaluates the whole binding if any of them signals a change. To make fully use of this, you should also expose hasError as a property rather than a function (and emit whenever it changes).
Follow-up update
Yes, you can make it as wild as you want. I think you mean this:
Image {
source: {
if(weather.data['dt']['sunrise'] <= weather.data['dt']['sunset'])
return "night.png"
else if(weather.data['dt']['sunrise'] > weather.data['dt']['sunset'])
return "day.png"
else
return "" //means no image
}
}

How to use the widget fullpath to access widget's properties?

I know that using widget.getFullPath() method, we can retrieve the full path of a widget as a string in the format "pagename.child1name.child2name....childNname.widgetname".
I am trying to find a way for using this string to access the original widget.
I was successful using this:
app.pages["pagename"].children["child1name"].children["child2name"]....children["childNname"].children["widgetname"].text = "some text";
but I don't know how to change this in a more general function, independent by the depth of the path.
Any idea would be very much appreciated. Thank you.
The parent root descendents should have everything on the page, so try:
widget.parent.root.descendants.ANYTHING.value;
you might alternately need to do this if you want ANYTHING to be a variable
widget.parent.root.descendants[ANYTHING].value;
My intention is to access the widget's property from another page. The root of a widget is its page, and it is the string before the first dot in the full path.
I eventually wrote a function which builds the object after splitting the full path string in an array:
function getWidgetPath(pathString) {
var pathArray = pathString.split('.');
var widgetPath = app.pages[pathArray[0]];
for( i=1; i<pathArray.length; i++ ) {
widgetPath = widgetPath.children[pathArray[i]];
}
return widgetPath;
}
The function can be used to build the target object. I.e., considering that the target widget is a label and I want to change its text property, I use:
var targetPath = 'parentpage.panel1.panel2.panel3.label1';
var targetWidget = getWidgetPath(targetPath);
targetWidget.text = 'some text';
I have the feeling that it's not very Javascriptian, though.

How to do empty list check in maximo anywhere

I am getting few json response from service as "-##EmptyComplexField##-". Due to which screen crashes. How to do a proper empty check for complex feild in maximo anywhere. Please help with sample code.
We can do a empty check for list as below
if(list && list!="-##EmptyComplexField##-")
Not sure why you're getting this EmptyComplexField string down at your layer. Are you sure you've defined your multiplicity correctly on the OSLC Resources app for these new children objects?
if (/-or-many$/.test(complexAttribute.multiplicity)){
var attributeName = complexAttribute.remoteName;
if (!(attributeName in data)){
Are you making sure these complexattributes are set as requiredattributes on the view before you display it?
The most correct answer would be:
if (list && list != PlatformConstants.EMPTY_COMPLEX_FIELD) { ... }

How to bind a SAPUI5 control property with data out of a binding?

From time to time I have the requirement to bind a control property to based on data out of model A to another model B.
For example the syntax could look like this (but will not work):
text : "{B>/rootB/{A>someValue}/propertyB}"
I normally solve this problem by "misusing" an unused control property in combination with the format function. It would look like this:
tooltip : {
path : "A>someValue",
formatter : function(oValue) {
// do some checks on oValue
var path = "B>/rootB/"+oValue+"/propertyB";
this.bindProperty("text", path);
return undefined; // because tooltip is not used
}
The benefit of this, each time "A>someValue" will be changed the binding of "text" will be updated automatically.
It is also possible to do this in template code (like items aggregations).
But you may smell the code ;)
Any suggestions to make it cleaner?
As far as I know, there is no such possibility in UI5 (yet). I always use a formatter function as you already mentioned. I say not YET, because developers seem to be aware of this feature request: see on GitHub
BUT, you dont need to missuse a random control property! Just use the formatter to read the needed values from any model you have access to:
text : {
path : "A>someValue1",
formatter : function(oValue) {
// read model B to get someValue2 (based on someValue1)
var path = "B>/rootB/"+oValue+"/propertyB";
var B = getModel("someModel");
var someValue2 = B.getProperty(path);
return someValue2
}

Find broken images in page & image replace by another image

Hi I am using selenium webdriver 2.25.0 & faceing the some serious issues,
how to find broken images in a page using Selenium Webdriver
How to find the image is replace by another image having same name (This is also bug) using webdriver.
Thanks in advance for your value-able suggestions.
The accepted answer requires that you use a proxy with an extra call to each image to determine if the images are broken or not.
Fortunately, there is another way you can do this using only javascript (I'm using Ruby, but you can use the same code in any executeScript method across the WebDriver bindings):
images = #driver.find_elements(:tag_name => "img")
broken_images = images.reject do |image|
#driver.execute_script("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", image)
end
# broken_images now has an array of any images on the page with broken links
# and we want to ensure that it doesn't have any items
assert broken_images.empty?
To your other question, I would recommend just taking a screenshot of the page and having a human manually verify the resulting screenshot has the correct images. Computers can do the automation work, but humans do have to check and verify its results from time to time :)
The next lines are not optimized, but they could find broken images:
List<WebElement> imagesList = _driver.findElements(By.tagName("img"));
for (WebElement image : imagesList)
{
HttpResponse response = new DefaultHttpClient().execute(new HttpGet(image.getAttribute("src");));
if (response.getStatusLine().getStatusCode() != 200)
// Do whatever you want with broken images
}
Regarding your second issue, I think I didn't understand it correctly. Could you explain it with more detail?
Based on the other answers, the code that eventually worked for me in an angular / protractor / webdriverjs setting is:
it('should find all images', function () {
var allImgElts = element.all(by.tagName('img'));
browser.executeAsyncScript(function (callback) {
var imgs = document.getElementsByTagName('img'),
loaded = 0;
for (var i = 0; i < imgs.length; i++) {
if (imgs[i].naturalWidth > 0) {
loaded = loaded + 1;
};
};
callback(loaded);
}).then(function (loadedImagesCount) {
expect(loadedImagesCount).toBe(allImgElts.count());
});
});
The webdriver code counts the number of img elements, and the function executed within the browser context counts the number of successfully loaded elements. These numbers should be the same.

Resources