Dynamic changes in resources, Windows Phone - wpf

I'm making some changes to resources in runtime to prepare application for different (physical) screen sizes:
var info = DisplayInformation.GetForCurrentView();
Size screenSize = new Size(Math.Round(Window.Current.Bounds.Width * info.RawPixelsPerViewPixel),
Math.Round(Window.Current.Bounds.Height * info.RawPixelsPerViewPixel));
Application.Current.Resources["SmallerText"] = (1.0 / 15.0) * screenSize.Height / info.RawDpiY * 72;
Application.Current.Resources["SmallText"] = (1.0 / 12.0) * screenSize.Height / info.RawDpiY * 72;
Application.Current.Resources["MediumText"] = (1.0 / 10.0) * screenSize.Height / info.RawDpiY * 72;
Application.Current.Resources["LargeText"] = (1.0 / 5.0) * screenSize.Height / info.RawDpiY * 72;
var displayMargin = (1.0 / 320.0) * screenSize.Height;
Application.Current.Resources["DisplayMargin"] = new Thickness(displayMargin);
Now when I try to use modified font sizes, it works:
<Style TargetType="Button" x:Key="BaseButton">
<Setter Property="FontSize" Value="{StaticResource SmallText}" />
But when I try to use modified Thickness, application crashes:
<Grid Grid.Row="0" Margin="{StaticResource DisplayMargin}">
Error message is quite enigmatic:
An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in MyApp.exe but was not handled in user code
WinRT information: Failed to assign to property '%0'. [Line: 17 Position: 50]
Additional information: The text associated with this error code could not be found.
Failed to assign to property '%0'. [Line: 17 Position: 50]
If there is a handler for this exception, the program may be safely continued.
Why can't I use modified Thickness? If I don't modify it from the code behind, everything works correctly.
Edit: Workarounds
Workaround #1:
var displayMargin = (1.0 / 320.0) * screenSize.Height;
Application.Current.Resources.Remove("DisplayMargin");
Application.Current.Resources.Add("DisplayMargin", new Thickness(displayMargin));
Workaround #2:
<Grid Grid.Row="0" Margin="{ThemeResource DisplayMargin}">

Related

PowerApps Distance Between Coordinates in a Loop

I am new to PowerApps. I have a Share Point List with a set of coordinates for various locations. What I need to figure out is when a user with PowerApps App on their phone are within a certain distance of those coordinates, using the Haversine formula. While the Haversine formula works in a test/simple way using hard-coded target location, I need the target locations from the Share Point List. Here is my code, which is inside a Timer End:
ForAll(
irfan_yard_locations,
With(
{
r: 6371000, p: (Pi() / 180), latA: location_lat, lonA: location_long, latB: Location.Latitude, lonB: Location.Longitude
},
Round((2 * r) * Asin(Sqrt(0.5 - Cos((latA - latB) * p)/2 + Cos(latB * p) * Cos(latA * p) * (1 - Cos((lonA - lonB) * p)) / 2)), 2 ));
) ;
irfan_yard_locations is the SP List which has location_lat and location_long coordinates. I am thinking something like in this example site: http://powerappsguide.com/blog/post/formula-how-to-use-the-if-and-switch-functions so that if the return from the with (), which would be a number, is less than 5 (it's in meters) then Navigate to another screen or do something. Just don't know the syntax. Or is there better approach?
Thanks!
Never mind. By putting in an If clause around the With function, I am able to catch if the current location is within 6 meters of one of the coordinate sets.
ForAll(
irfan_yard_locations,
If ( With(
{
r: 6371000, p: (Pi() / 180), latA: location_lat, lonA: location_long, latB: Location.Latitude, lonB: Location.Longitude
},
Round((2 * r) * Asin(Sqrt(0.5 - Cos((latA - latB) * p)/2 + Cos(latB * p) * Cos(latA * p) * (1 - Cos((lonA - lonB) * p)) / 2)), 2 )) < 6, Navigate(Screen2));
) ;

Create bounding box from center coordinate

I'm trying to create a bounding box at a specific scale from a center coordinate. I'm trying to keep it within the aspect ratio of a 8.5x11inch piece of paper (612x792 pixels # 72dpi).
The code I'm using below mostly works, but the heigh seems a bit too tall for the aspect ratio of a letter. Am I not accounting for mercator projection? What am I missing here?
def bounding_box_from_point(center:, size:, scale_denominator:)
dpi = 72
inches_per_unit = 4374754
resolution = 1 / (scale_denominator * inches_per_unit * dpi)
half_width_deg = (size.width * resolution) / 2
half_height_deg = (size.height * resolution) / 2
BoundingBox.new(
north: center.lat + half_height_deg,
south: center.lat - half_height_deg,
east: center.lon + half_width_deg,
west: center.lon - half_width_deg
)
end
Calling bounding_box_from_point(center: center, size: size, scale_denominator: scale) with:
scale = 0.0008861342166177423 (i.e. 1/18055.955520)
center = Geometry::Location.new(lat: 37.806336, lon: -122.270625)
size.width = 612,
size.height = 792
It returns:
west: -122.27172131608657,
east: -122.26952868391342,
south: 37.804917238005615
north: 37.80775476199439
If you go to http://www.openstreetmap.org/export and enter those bounding box coordinates, you can see that the ratio does not match that of a 8.5x11in piece of paper...it's slightly too tall. What am I doing wrong here or not understanding?
This was my implementation that solved it!
def self.bounding_box_from_location(location:, scale:)
scale_meters_per_pixel = 0.0003
# scale.zoom is an integer between 0-19
# See http://wiki.openstreetmap.org/wiki/Zoom_levels for a description on `zoom`
# location.lat_rad is the latitude in radians, same for lon_rad
meters_per_pixel = EARTH_CIR * Math.cos(location.lat_rad) / (2 ** (scale.zoom + 8))
earth_meters_per_pictureMeters = meters_per_pixel / scale_meters_per_pixel
# height/width in meters is the height/width of the box you're applying this to
# In my case I'm using the heigh/width of a Letter of paper
# You can convert Pixels to meters (# 72dpi) with
# pixels * 0.00035277777777777776
meters_north = earth_meters_per_pictureMeters * scale.height_in_meters / 2
meters_east = earth_meters_per_pictureMeters * scale.width_in_meters / 2
meters_per_degree_lat = 111111.0
meters_per_degree_long = 111111.0 * Math.cos(location.lat_rad)
degrees_north = meters_north / meters_per_degree_lat
degrees_east = meters_east / meters_per_degree_long
BoundingBox.new(
north: (location.lat + degrees_north),
south: location.lat - degrees_north,
east: location.lon + degrees_east,
west: location.lon - degrees_east,
width: scale.width_in_pixels,
height: scale.height_in_pixels
)
end

RoundMask gives exception when image of specific size is used

I have got a quite interesting problem here. While using round Mask, it was working all fine but i found a issue in my device. So I checked in the simulator (iOS 6) and found that if I use image of size 480 * 480 or 485 * 485, it gives "Mask and image sizes don't match" error. I change the size to 470 * 470 and 500 * 500, it works perfectly. Moreover 480*475 works fine too. Square img with specific sizes are giving the errors eg:694*694, 690*690 gives error. The display width of the simulator shows 750 in the output.
System.out.println("width " + Display.getInstance().getDisplayWidth()); //output = 750
if (profile_img != null && !"".equals(profile_img)) {
Image roundMask = Image.createImage(placeholderForProfile.getWidth(), placeholderForProfile.getHeight(), 0xff000000);
roundMask = roundMask.scaledWidth(screenWidth / 3);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, placeholderForProfile.getWidth(), placeholderForProfile.getHeight(), 0, 360);
URLImage.ImageAdapter ada = URLImage.createMaskAdapter(roundMask);
AllUrl au = new AllUrl();
Image getProfileImage = URLImage.createToStorage(placeholderForProfile, "profile.png",
au.profileImgUrl + profile_img, ada);
profileImg.setIcon(getProfileImage);
}
Error in simulator:
java.lang.IllegalArgumentException: Mask and image sizes don't match
[EDT] 0:8:44,805 - Exception in AppName version 1.01
[EDT] 0:8:44,805 - OS ios
[EDT] 0:8:44,805 - Error java.lang.IllegalArgumentException: Mask and image sizes don't match
[EDT] 0:8:44,805 - Current Form Profile
[EDT] 0:8:44,805 - Exception: java.lang.IllegalArgumentException - Mask and image sizes don't match
at com.codename1.ui.Image.applyMask(Image.java:279)
at com.codename1.ui.URLImage$3.postProcess(URLImage.java:180)
at com.codename1.ui.URLImage$ScaleToFill.adaptImage(URLImage.java:119)
at com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:214)
at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
at com.codename1.ui.util.EventDispatcher.access$100(EventDispatcher.java:45)
at com.codename1.ui.util.EventDispatcher$CallbackClass.run(EventDispatcher.java:95)
Rendering frame took too long 166 milliseconds
at com.codename1.ui.Display.processSerialCalls(Display.java:1151)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1095)
at com.codename1.ui.Display.mainEDTLoop(Display.java:996)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.lang.IllegalArgumentException: Mask and image sizes don't match
at com.codename1.ui.Image.applyMask(Image.java:279)
at com.codename1.ui.URLImage$3.postProcess(URLImage.java:180)
at com.codename1.ui.URLImage$ScaleToFill.adaptImage(URLImage.java:119)
at com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:214)
at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
at com.codename1.ui.util.EventDispatcher.access$100(EventDispatcher.java:45)
at com.codename1.ui.util.EventDispatcher$CallbackClass.run(EventDispatcher.java:95)
at com.codename1.ui.Display.processSerialCalls(Display.java:1151)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1095)
at com.codename1.ui.Display.mainEDTLoop(Display.java:996)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
The mask and the placeholder should match in size exactly. You invoke scaled on the mask which might have no effect if the sizes are already the same but will fail badly if they aren't.

A value in x_new is above/below interpolation range

Here's my code. I keep getting that error, no matter if I use inside_sig_r or inside_sig_r2. If I take out the last line I don't get this error. Thanks for any help.
import numpy as np
from scipy import integrate, interpolate
from math import sqrt,pi,cos,sin,exp
rho = 2.78e11 * .3
delta = 1.68
def M(r):
return ((4*pi)/3)*(r**3)*rho
def R(M):
return (( 3 * M) / (4 * pi * rho)) ** (.3333)
def W(x):
return 3.*(sin(x) - x*cos(x))/(x**3.)
data = np.loadtxt("//Users//Slemons//Downloads//pk2.dat", float)
k_data = (10**data[:,0])
Pk_data = (10**data[:,1])
Pk = interpolate.interp1d(k_data,Pk_data,kind='cubic')
Masses = np.arange(1e10,1e16,1e10)
r_from_M = np.array(map(R,Masses),float)
print r_from_M[0]
def inside_sig_r(k,r):
return ((W(k*r)**2.) * Pk(k) * (k**2.)) / (2. * (pi ** 2.))
def inside_sig_r2(z,r):
k = (1.+z)/(1.-z)
return inside_sig_r(k,r) *2./(1.-z)**2.
sigma_r = lambda k : sqrt(integrate.quad(inside_sig_r,.4,np.inf,args=(k)))
sigr = np.array(map(sigma_r,Masses),float)
It is not really a standalone code, since I can't see your data in '//Users//Slemons//Downloads//pk2.dat'. However, the error is quite specific - the data produced by inside_sig_r(2) are above or below the range you supplied (0.4, np.inf). You should either change the range or make sure that the functions return values inside this range.

Why is this Objective C drawing method leaking?

can anybody tell me why this method, which is called very often, is leaking memory?
If I take a look at the iOS Allocation Tool / VM Tool there are no leaks .... but if I look at a report_memory function which I found at here at stackoverflow, I can see the that the resident size is growing by 1 MB per 2 seconds. If I don't call this method the resident size is only growing by 1 MB per 40 seconds. At some time I receive a "Did receive memory warning" Log, but I cant figure out why this is happening. Resident Size, Dirty Size, Allocations ... everything looks alright.
path2 is a class Variable.
-(void) drawPath:(float) winkel path:(UIBezierPath *) mpath toPoint:(CGPoint) pt{
path2 = [UIBezierPath bezierPathWithCGPath:mpath.CGPath];
box = CGPathGetPathBoundingBox(path2.CGPath);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-1 * (box.origin.x + (box.size.width / 2)), -1 * (box.origin.y + (box.size.height / 2)));
[path2 applyTransform:translate];
CGAffineTransform rotate = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(winkel));
[path2 applyTransform:rotate];
translate = CGAffineTransformMakeTranslation((box.origin.x + (box.size.width / 2)), (box.origin.y + (box.size.height / 2)));
[path2 applyTransform:translate];
translate = CGAffineTransformMakeTranslation(pt.x-(box.size.width / 2), pt.y-(box.size.height / 2));
[path2 applyTransform:translate];
[path2 fill];
}
I think the problem is CGAffineTransformMakeTranslation/applyTransform ... but I cant figure out why this method is leaking.
Every Core Foundation object of returned by a function that has Make in the name, must be explicitly released using CFRelease.

Resources